QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112409#6503. DFS Order 3crzsty#TL 0ms7584kbC++141.8kb2023-06-11 16:44:272023-06-11 16:44:31

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-11 16:44:31]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:7584kb
  • [2023-06-11 16:44:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int M=1010;
int n;
pair <int,int> G[M][M];
//pair <int,int> NG[M][M];
int mp[M][M];
pair <int,int> ans[M];
int tot,tim;
int f[M];
int findf(int a){
	return f[a]==a?a:f[a]=findf(f[a]);
}
void addans(int a,int b){
	if(mp[a][b]!=tim){
		mp[a][b]=mp[b][a]=tim;
		ans[++tot]=make_pair(a,b);
	}
}
int had[M],idx[M];
int hadb[M][M];
int topp;
pair <int,int> hb[M];
void solve(int coln){
	if(coln==1) return;
	int next_coln=coln;
	topp=0;
	for(int i=1,a,b;i<=n;i++){
		if(coln==n){
			addans(G[i][1].first,G[i][2].first);
			a=findf(G[i][1].first);
			b=findf(G[i][2].first);
			if(a^b) f[a]=b,--next_coln;
		}else{
			a=G[i][1].second;
			b=G[i][2].second;
			if(hadb[a][b]==coln) continue;
			hadb[a][b]=hadb[b][a]=coln;
			hb[++topp]=make_pair(a,b);
			int ii=b;
			for(int j=2;j<=coln;j++){
				if(G[ii][j].second==a){
					addans(G[i][2].first,G[ii][j].first);
					break;
				}
			}
		}
	}
	for(int i=1,a,b;i<=topp;i++){
		a=findf(hb[i].first);
		b=findf(hb[i].second);
		if(a==b) continue;
		f[a]=b;--next_coln;
	}
	for(int i=1;i<=coln;i++)had[i]=0;
	for(int i=1;i<=n;i++){
		int nowt=0;
		for(int j=1,a;j<=coln;j++){
			a=findf(G[i][j].first);
			if(had[a]!=i){
				had[a]=i;
				G[i][++nowt].first=G[i][j].first;
				G[i][nowt].second=a;
			}
		}
	}
	solve(next_coln);
}
vector <int> A;
int dfs(int a){
	if(a<=-10) return 0;
	return A[a]+dfs(a-1);
}
int main(){
	int T;
	A.resize(2);
	for(scanf("%d",&T);T;T--){
		scanf("%d",&n);
		tot=0;
		++tim;
		for(int i=1;i<=n;i++){
			f[i]=i;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				scanf("%d",&G[i][j].first);
				G[i][j].second=G[i][j].first;
			}
		}
		solve(n);
		if(tot!=n-1) dfs(1);
		for(int i=1;i<=tot;i++){
			printf("%d %d\n",ans[i].first,ans[i].second);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7584kb

input:

4
2
1 2
2 1
3
1 2 3
2 1 3
3 2 1
4
1 2 3 4
2 1 3 4
3 2 4 1
4 2 1 3
5
1 2 4 3 5
2 4 1 3 5
3 5 1 2 4
4 2 1 3 5
5 3 1 2 4

output:

1 2
1 2
3 2
1 2
3 2
4 2
1 2
2 4
3 5
3 1

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

20000
10
1 2 4 5 6 7 3 8 10 9
2 1 4 5 6 7 3 8 10 9
3 8 1 2 4 5 6 7 10 9
4 5 6 7 1 2 3 8 10 9
5 4 6 7 1 2 3 8 10 9
6 7 4 5 1 2 3 8 10 9
7 6 4 5 1 2 3 8 10 9
8 3 1 2 4 5 6 7 10 9
9 10 1 2 4 5 6 7 3 8
10 1 2 4 5 6 7 3 8 9
10
1 4 3 8 2 9 6 5 7 10
2 8 9 6 3 4 1 5 7 10
3 8 2 9 6 4 1 5 7 10
4 1 3 8 2 9 6 5...

output:


result: