QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#871928#9881. Diverge and ConvergeshungTL 0ms4352kbC++143.1kb2025-01-25 22:50:102025-01-25 22:50:10

Judging History

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

  • [2025-01-25 22:50:10]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:4352kb
  • [2025-01-25 22:50:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int n,deg1[N],deg2[N],ans1[N],ans2[N],now,son[N][2][2],v[N],r[N],aa[N],tot,sta[N][2],top,X1[N],Y1[N],X2[N],Y2[N];
bool vis[N],bo[N];
map<int,int>mp1[N],mp2[N];
inline void dfs1(int x,int fa){
	bo[x]=1;
	auto it=mp1[x].begin();
	while(it!=mp1[x].end()){
		int y=(*it).first;
		if(y!=fa)dfs1(y,x);
		++it;
	}
}
inline void dfs2(int x,int fa){
	bo[x]=1;
	auto it=mp2[x].begin();
	while(it!=mp2[x].end()){
		int y=(*it).first;
		if(y!=fa)dfs2(y,x);
		++it;
	}
}
inline void work(int x,int y){
	if(x>y&&son[x][0][0]){
		memset(bo,0,sizeof(bo));
		dfs1(son[x][0][1],r[x]);
		if(bo[aa[x]]){
			work(son[x][0][0],v[x]);
			work(son[x][1][0],y);
		}
		else{
			work(son[x][1][0],v[x]);
			work(son[x][0][0],y); 
		}
	}
	else if(son[y][0][0]){
		memset(bo,0,sizeof(bo));
		dfs2(son[y][0][1],r[y]);
		if(bo[aa[y]]){
			work(v[y],son[y][0][0]);
			work(x,son[y][1][0]);
		}
		else{
			work(v[y],son[y][1][0]);
			work(x,son[y][0][0]); 
		}
	}
	else{
		++now;
		ans1[now]=x,ans2[now]=y;
		mp1[X1[x]].erase(Y1[x]),mp1[Y1[x]].erase(X1[x]),mp2[X2[y]].erase(Y2[y]),mp2[Y2[y]].erase(X2[y]);
		mp1[X2[y]][Y2[y]]=mp1[Y2[y]][X2[y]]=x,mp2[X1[x]][Y1[x]]=mp2[Y1[x]][X1[x]]=y;
	}
}
int main(){
	scanf("%d",&n);
	int x,y;
	for(int i=1;i<n;++i){
		scanf("%d%d",&x,&y);
		X1[i]=x,Y1[i]=y;
		mp1[x][y]=mp1[y][x]=i;
		++deg1[x],++deg1[y];
	}
	for(int i=1;i<n;++i){
		scanf("%d%d",&x,&y);
		X2[i]=x,Y2[i]=y;
		mp2[x][y]=mp2[y][x]=i;
		++deg2[x],++deg2[y];
	}
	tot=n+n;
	for(int t=1;t<n;++t){
		x=1;
		for(int i=2;i<=n;++i)if(!vis[i]&&(vis[x]||deg1[i]+deg2[i]<deg1[x]+deg2[x]))x=i;
		vis[x]=1;
		if(deg1[x]+deg2[x]==2){
			auto i1=mp1[x].begin();
			y=(*i1).first;
			int j=(*i1).second;
			mp1[y].erase(x);
			--deg1[y];
			auto i2=mp2[x].begin();
			y=(*i2).first;
			int k=(*i2).second;
			mp2[y].erase(x);
			--deg2[y];
			++top;
			sta[top][0]=j,sta[top][1]=k;
		}
		else{
			if(deg1[x]==1){
				auto i1=mp1[x].begin();
				int a=(*i1).first;
				mp1[a].erase(x);
				--deg1[a];
				int j=(*i1).second;
				auto i2=mp2[x].begin();
				int b=(*i2).first,B=(*i2).second;
				++i2;
				int c=(*i2).first,C=(*i2).second;
				mp2[b].erase(x),mp2[c].erase(x);
				mp2[b][c]=mp2[c][b]=++tot;
				son[tot][0][0]=B,son[tot][0][1]=b,son[tot][1][0]=C,son[tot][1][1]=c,v[tot]=j,r[tot]=x,aa[tot]=a;
			}
			else{
				auto i2=mp2[x].begin();
				int a=(*i2).first;
				mp2[a].erase(x);
				--deg2[a];
				int j=(*i2).second;
				auto i1=mp1[x].begin();
				int b=(*i1).first,B=(*i1).second;
				++i1;
				int c=(*i1).first,C=(*i1).second;
				mp1[b].erase(x),mp1[c].erase(x);
				mp1[b][c]=mp1[c][b]=++tot;
				son[tot][0][0]=B,son[tot][0][1]=b,son[tot][1][0]=C,son[tot][1][1]=c,v[tot]=j,r[tot]=x,aa[tot]=a;
			}
		}
	}
	for(x=1;x<=n;++x)mp1[x].clear(),mp2[x].clear();
	for(int i=1;i<n;++i)mp1[X1[i]][Y1[i]]=mp1[Y1[i]][X1[i]]=mp2[X2[i]][Y2[i]]=mp2[Y2[i]][X2[i]]=i;
	while(top){
		work(sta[top][0],sta[top][1]);
		--top;
	}
	for(int i=1;i<n;++i)printf("%d ",ans1[i]);
	printf("\n");
	for(int i=1;i<n;++i)printf("%d ",ans2[i]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2 3 1 
3 2 1 

result:

ok Correct, N = 4

Test #2:

score: 0
Accepted
time: 0ms
memory: 4352kb

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 4352kb

input:

7
1 2
1 3
2 4
2 5
3 6
3 7
1 5
1 6
1 7
5 2
6 3
7 4

output:

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

result:

ok Correct, N = 7

Test #4:

score: -100
Time Limit Exceeded

input:

1000
780 67
364 281
934 245
121 472
460 233
574 534
91 687
91 832
413 613
815 638
519 196
992 120
150 157
198 365
643 700
279 698
623 215
578 330
869 333
874 570
879 697
897 671
962 670
108 137
779 9
988 91
919 314
696 722
431 270
810 692
769 49
254 915
737 580
229 888
379 612
19 161
787 346
280 753...

output:


result: