QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865601#9881. Diverge and ConvergeshungWA 5ms4224kbC++142.1kb2025-01-21 20:16:192025-01-21 20:16:19

Judging History

This is the latest submission verdict.

  • [2025-01-21 20:16:19]
  • Judged
  • Verdict: WA
  • Time: 5ms
  • Memory: 4224kb
  • [2025-01-21 20:16:19]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2005;
int n,deg1[N],deg2[N],ans1[N],ans2[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;
	}
}
int main(){
	scanf("%d",&n);
	int x,y;
	for(int i=1;i<n;++i){
		scanf("%d%d",&x,&y);
		mp1[x][y]=mp1[y][x]=i;
		++deg1[x],++deg1[y];
	}
	for(int i=1;i<n;++i){
		scanf("%d%d",&x,&y);
		mp2[x][y]=mp2[y][x]=i;
		++deg2[x],++deg2[y];
	}
	for(int t=1;t<n;++t){
		x=1;
		while(vis[x]||deg1[x]+deg2[x]>3)++x;
		vis[x]=1;
		if(deg1[x]+deg2[x]==2){
			auto i1=mp1[x].begin();
			y=(*i1).first;
			mp1[y].erase(x);
			--deg1[y];
			ans1[t]=(*i1).second;
			auto i2=mp2[x].begin();
			y=(*i2).first;
			mp2[y].erase(x);
			--deg2[y];
			ans2[t]=(*i2).second;
		}
		else{
			if(deg1[x]==1){
				auto i1=mp1[x].begin();
				int a=(*i1).first;
				mp1[a].erase(x);
				--deg1[a];
				ans1[t]=(*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);
				memset(bo,0,sizeof(bo));
				dfs2(b,x);
				if(bo[a]){
					ans2[t]=B;
					mp2[b][c]=mp2[c][b]=C;
				}
				else{
					ans2[t]=C;
					mp2[b][c]=mp2[c][b]=B;
				}
			}
			else{
				auto i2=mp2[x].begin();
				int a=(*i2).first;
				mp2[a].erase(x);
				--deg2[a];
				ans2[t]=(*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);
				memset(bo,0,sizeof(bo));
				dfs1(b,x);
				if(bo[a]){
					ans1[t]=B;
					mp1[b][c]=mp1[c][b]=C;
				}
				else{
					ans1[t]=C;
					mp1[b][c]=mp1[c][b]=B;
				}
			}
		}
	}
	for(int i=1;i<n;++i)printf("%d ",ans1[i]);
	printf("\n");
	for(int i=1;i<n;++i)printf("%d ",ans2[i]);
}

详细

Test #1:

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

input:

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

output:

1 2 3 
1 3 2 

result:

ok Correct, N = 4

Test #2:

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

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

score: 0
Accepted
time: 1ms
memory: 4096kb

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

result:

ok Correct, N = 7

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 4224kb

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:

249 575 25 952 889 775 67 286 586 411 424 925 466 614 440 798 116 493 210 549 1 843 709 569 613 548 907 276 142 332 819 463 457 12 562 39 682 816 397 392 100 24 407 904 807 591 626 603 388 149 13 384 271 921 36 304 235 366 272 958 559 838 557 431 505 596 104 191 515 70 336 470 986 892 316 11 858 432...

result:

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