QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112417#6503. DFS Order 3crzsty#WA 2ms5584kbC++142.1kb2023-06-11 17:23:242023-06-11 17:23:27

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 17:23:27]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5584kb
  • [2023-06-11 17:23:24]
  • 提交

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],res[M];
int topp;
pair <int,int> hb[M];
void solve(int hcnt,int coln){
	if(hcnt==1||coln==1) return;
	int next_coln=coln;
	topp=0;
	for(int i=1,a,b;i<=hcnt;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)swap(a,b);
			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=idx[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;
		if(b>a)swap(a,b);
		f[a]=b;--next_coln;
	}
	int nowup=0;
	for(int i=1;i<=n;i++){
		if(findf(i)==i){
			res[++nowup]=i;
		}
	}
	for(int i=1;i<=n;i++)had[i]=0;
	for(int i=1;i<=nowup;i++){
		int h=idx[res[i]];
		int nowt=0;
		for(int j=1;j<=coln;j++){
			int a=findf(G[h][j].first);
			if(had[a]!=i){
				had[a]=i;
				G[i][++nowt].first=G[h][j].first;
				G[i][nowt].second=a;
			}
		}
	}
	for(int i=1;i<=n;i++){
		idx[res[i]]=i;
	}
	solve(nowup,next_coln);
}
int main(){
	int T;
	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;
			}
		}
		for(int i=1;i<=n;i++)idx[i]=i;
		solve(n,n);
		if(tot!=n-1) return 0;
		for(int i=1;i<=tot;i++){
			printf("%d %d\n",ans[i].first,ans[i].second);
		}
	}
	return 0;
}
/*
1
7
1 2 7 3 5 4 6
2 7 1 3 4 6 5
3 5 4 6 1 2 7
4 6 3 1 2 7 5
5 3 1 2 7 4 6
6 4 3 5 1 2 7
7 2 1 3 5 4 6
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5572kb

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
Wrong Answer
time: 1ms
memory: 5584kb

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:

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

result:

wrong output format Unexpected end of file - int32 expected (test case 2)