QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820594#9881. Diverge and ConvergeNKheyuxiangWA 16ms3988kbC++142.8kb2024-12-18 22:03:172024-12-18 22:03:17

Judging History

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

  • [2024-12-18 22:03:17]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:3988kb
  • [2024-12-18 22:03:17]
  • 提交

answer

#include<bits/stdc++.h>
#define N 2005
using namespace std;
int n,r0[N],r1[N],c0,c1;
int a0[N],b0[N],a1[N],b1[N];
bool vp[N],vis[N];
vector<int > vec[N];
int mat[N],lft[N],rht[N],mt[N];
bool est0(int i){return (!vp[a0[i]])&&(!vp[b0[i]]);}
bool est1(int i){return (!vp[a1[i]])&&(!vp[b1[i]]);}
int inc0(int i,int x){
	if(!est0(i)||((a0[i]^x)&&(b0[i]^x))) return 0;
	return a0[i]+b0[i]-x;
}
int inc1(int i,int x){
	if(!est1(i)||((a1[i]^x)&&(b1[i]^x))) return 0;
	return a1[i]+b1[i]-x;
}
void ins(int x,int y){
	rht[y]=rht[x];
	lft[rht[x]]=y;
	rht[x]=y;
	lft[y]=x;
}
void rpl(int x,int y){
	int pr=lft[x];
	rht[pr]=rht[x];
	lft[rht[x]]=pr;
	ins(pr,y);
}
void jb(int u,int v){
	vec[u].push_back(v);
	vec[v].push_back(u); 
}
void run(int u,int fa){
	vis[u]=1;
	for(int x:vec[u]) if(x!=fa) run(x,u);
}
void dfs(int d){
	if(d==1) return;
	int p=0;
	for(int i=1;i<=n&&!p;i++)
		if(!vp[i]&&r0[i]+r1[i]<=3) p=i;
//	cout<<d<<" "<<p<<endl;
	if(r0[p]+r1[p]<=2){
		int x=0,ex=0,y=0,ey=0;
		for(int i=1;i<=c0&&!x;i++)
			if((x=inc0(i,p))) ex=i;
		for(int i=1;i<=c1&&!y;i++)
			if((y=inc1(i,p))) ey=i;
		r0[x]--,r1[y]--;
//		cout<<": "<<d<<" "<<p<<" "<<x<<" "<<y<<" "<<ex<<" "<<ey<<endl;
		vp[p]=1;
		dfs(d-1);
		ins(0,ex);mat[ex]=ey;mt[ey]=ex;
	}
	else if(r0[p]==2){
		int x=0,ex=0,y=0,ey=0,z=0,ez=0;
//		for(int i=1;i<=n;i++) cout<<vp[i]<<endl;
		for(int i=1;i<=c0&&!y;i++){
			if(!x) x=inc0(i,p),ex=i;
			else y=inc0(i,p),ey=i;
		}
		for(int i=1;i<=c1&&!z;i++)
			if((z=inc1(i,p))) ez=i;
		r1[z]--;
		int id=++c0;a0[id]=x,b0[id]=y;
//		cout<<": "<<d<<" "<<p<<" "<<x<<" "<<y<<" "<<z<<endl;
		vp[p]=1;
		dfs(d-1);
		int em=mat[id];
		for(int i=1;i<=c1;i++)
			if(est1(i)&&i!=em) jb(a1[i],b1[i]);
		for(int i=1;i<=n;i++) vis[i]=0;
		run(z,0);
		for(int i=1;i<=n;i++) vec[i].clear();
		if(vis[y]) swap(x,y),swap(ex,ey);
		rpl(id,ey);mat[ey]=em;mt[em]=ey;
		ins(ey,ex);mat[ex]=ez;mt[ez]=ex;
		c0--;
	}
	else{
		int x=0,ex=0,y=0,ey=0,z=0,ez=0;
		for(int i=1;i<=c1&&!y;i++)
			if(!x) x=inc1(i,p),ex=i;
			else y=inc1(i,p),ey=i;
		for(int i=1;i<=c0&&!z;i++)
			if((z=inc0(i,p))) ez=i;
		r0[z]--;
		int id=++c1;a1[id]=x,b1[id]=y;
		vp[p]=1;
		dfs(d-1);
		int em=mt[id];
		for(int i=1;i<=c0;i++)
			if(est0(i)&&i!=em) jb(a0[i],b0[i]);
		for(int i=1;i<=n;i++) vis[i]=0;
		run(z,0);
		for(int i=1;i<=n;i++) vec[i].clear();
		if(vis[y]) swap(x,y),swap(ex,ey);
		mat[em]=ey;mt[ey]=em;
		ins(em,ez);mat[ez]=ex;mt[ex]=ez;
		c1--;
	}
	vp[p]=0;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++) scanf("%d%d",&a0[i],&b0[i]),r0[a0[i]]++,r0[b0[i]]++;
	for(int i=1;i<n;i++) scanf("%d%d",&a1[i],&b1[i]),r1[a1[i]]++,r1[b1[i]]++;
	c0=c1=n-1;
	dfs(n);
//	cout<<"over\n";
	for(int i=rht[0];i!=0;i=rht[i]) printf("%d ",i);
	printf("\n");
	for(int i=rht[0];i!=0;i=rht[i]) printf("%d ",mat[i]);
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

1 3 2 
1 2 3 

result:

ok Correct, N = 4

Test #2:

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

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

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

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:

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

result:

ok Correct, N = 7

Test #4:

score: -100
Wrong Answer
time: 16ms
memory: 3988kb

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:

25 952 775 411 424 925 466 440 798 116 493 210 1 142 819 562 39 100 904 384 271 304 596 316 432 587 201 801 136 247 969 547 401 751 390 267 198 804 248 345 608 423 916 366 880 331 898 597 458 649 385 274 14 619 687 86 734 89 256 674 219 625 707 354 850 911 806 704 960 94 662 343 146 312 187 92 891 4...

result:

wrong answer Wrong, N = 1000, not forming a tree