关于此题的玄学TLE
查看原帖
关于此题的玄学TLE
218188
ParanoidMO楼主2022/5/16 17:23

按照第一篇题解计算的AC复杂度为 O(n+mlogm)

而我的代码复杂度是 O(nlogn^2+mlogm)

log10^10=33.219281......

然而就TLE了四个点......

我不理解

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9+7;
vector<int> G[maxn];
int n, m, tot, org, end;
int in[maxn], out[maxn], cnt1[10];
stack<int> ans;
map<pair<int, int> , int> cnt; 
void dfs(int x){
	int pt;
	for (int i=0; i<G[x].size(); i++){
		int v = G[x][i];
		if (!cnt[{x, v}]) continue;
		cnt[{x, v}] --;
		dfs(v);
	}
	ans.push(x);
}

int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for (int i=1; i<=m; i++){
		int x, y; cin>>x>>y;
		G[x].push_back(y);
		cnt[{x, y}] ++;
		in[y] ++;
		out[x] ++;
	}
	for(int i=1; i<=n; i++) sort(G[i].begin(), G[i].end());
	for (int i=1; i<=n; i++){
		if (out[i] == in[i]+1 && !org) org = i;
		else if (out[i] == in[i]+1 && org){
			cout<<"No"<<endl;
			return 0;
		}
		if (in[i] == out[i]+1 && !end) end = i;
		else if (in[i] == out[i]+1 && end){
			cout<<"No"<<endl;
			return 0;
		}
		if (abs(in[i]-out[i]) > 1){
			cout<<"No"<<endl;
			return 0;
		}
	}
	if (!org) org = 1;
	dfs(org);
	while (ans.size()){
		int pt = ans.top(); ans.pop();
		cout<<pt<<" ";
	}
	
}
2022/5/16 17:23
加载中...