QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#144791#6503. DFS Order 3rsjTL 1ms11668kbC++141.5kb2023-08-21 18:50:592023-08-21 18:51:03

Judging History

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

  • [2023-08-21 18:51:03]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:11668kb
  • [2023-08-21 18:50:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N][N];
int id[N][N];
int vis[N][N];
struct edge {
	int to;
	edge *nex;
}*head[N];

int f[N][N],g[N][N];
int find(int x,int f[]) {
	if(f[x]==x) return x;
	return f[x]=find(f[x],f);
}
void uni(int x,int y,int f[]) {
	f[find(x,f)]=find(y,f);
}
bool check(int u,int v,int f[]) {
	return find(u,f)==find(v,f);
}
int e=0;
bool add(int u,int v) {
	if(vis[u][v]) return 0;
	vis[u][v]=1,e++;
	if(u<v) cout<<u<<" "<<v<<endl;
	edge *cur=new edge;
	cur->to=v;
	cur->nex=head[u];
	head[u]=cur;
	return 1;
}
bool con(int u,int v) {
	return add(u,v)&add(v,u);
}
int dis[N];
void get() {
	e=0;
	int n,i,j;
	cin>>n;
	for(i=1;i<=n;i++) for(head[i]=0,dis[i]=0,j=1;j<=n;j++) vis[i][j]=0;
	for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>a[i][j],id[i][a[i][j]]=j;
	for(i=1;i<=n;i++) for(j=1;j<=n+3;j++) f[i][j]=g[i][j]=j;
	for(i=1;i<=n;i++) con(a[i][1],a[i][2]);
	for(i=1;i<=n;i++) {
		for(j=1;j<=n;j++) if(!dis[j]) break;
		if(j==n+1) j=1;
		int u=find(n,g[j]);
		u=a[j][u];
		
		dis[u]=1;
		for(j=1;j<=n;j++) {
			//cout<<"del "<<j<<","<<id[j][u]<<endl;
			uni(id[j][u],id[j][u]+1,f[j]);
			uni(id[j][u],id[j][u]-1,g[j]);
		}
		for(j=1;j<=n;j++) {
			if(dis[j]) continue;
 			int x=find(1,f[j]);
			int y=find(x+1,f[j]);
			if(e==2*n-2) return ;
			con(a[j][x],a[j][y]);
		}
	}
	if(e!=2*n-2) cout<<"lost"<<endl;
	return ;
}
int main() {
	// freopen("2.in","r",stdin);
	int T; cin>>T;
	while(T--) get();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 11668kb

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
2 3
1 2
2 3
2 4
1 2
2 4
3 5
1 3

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:

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

result: