QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820289#9881. Diverge and ConvergeFesdrerML 1ms3900kbC++172.6kb2024-12-18 20:47:162024-12-18 20:47:16

Judging History

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

  • [2024-12-18 20:47:16]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3900kb
  • [2024-12-18 20:47:16]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,vis[N];
unordered_multiset<int> e[5][N];
vector<pair<int,int>> ans[2];
pair<int,int> edge[2][N];
inline bool equ(pair<int,int> x,pair<int,int> y){
	if(x.first==y.first&&x.second==y.second)	return true;
	if(x.first==y.second&&x.second==y.first)	return true;
	return false;
}
inline void add(int op,int x,int y,bool flag=true){
	e[op][x].insert(y),e[op][y].insert(x);
	if(flag)	e[2][x].insert(y),e[2][y].insert(x);
}
inline void erase(int op,int x,int y,bool flag=true){
	e[op][x].erase(e[op][x].find(y)),e[op][y].erase(e[op][y].find(x));
	if(flag)	e[2][x].erase(e[2][x].find(y)),e[2][y].erase(e[2][y].find(x));
}
bool find(int op,int x,int fas,int z){
	if(x==z)	return true;
	for(int y:e[op][x]) if(y!=fas&&find(op,y,fas,z))	return true;
	return false;
}
void dfs(int _){
	if(_<=1)	return;
	int mn=n,x=0;
	for(int i=1;i<=n;i++)	if(!vis[i]&&int(e[2][i].size())<=mn)
		mn=int(e[2][i].size()),x=i;
	if(mn==2){
		int y=*e[0][x].begin(),z=*e[1][x].begin();
		vis[x]=1,erase(0,x,y),erase(1,x,z),dfs(_-1),add(0,x,y),add(1,x,z),vis[x]=0;
		ans[0].push_back({x,y}),ans[1].push_back({x,z});
	}
	else{
		assert(mn==3);
		int op,nop;
		if(e[0][x].size()==1)	op=0,nop=1;
		else	op=1,nop=0;
		int w=*e[op][x].begin(),y=*e[nop][x].begin(),z=*(++e[nop][x].begin());
		vis[x]=1,erase(op,x,w),erase(nop,x,y),erase(nop,x,z),add(nop,y,z);
		dfs(_-1);
		int p,q;
		for(int i=1;i<=n;i++)	e[3][i]=e[0][i],e[4][i]=e[1][i];
		int pal=0;
		for(int i=0;i<int(ans[0].size());i++){
			if(equ(ans[nop][i],{y,z})){
				p=ans[op][i].first,q=ans[op][i].second;
				pal=i;
				break;
			}
			erase(3,ans[0][i].first,ans[0][i].second,0);
			erase(4,ans[1][i].first,ans[1][i].second,0);
			add(4,ans[0][i].first,ans[0][i].second,0);
			add(3,ans[1][i].first,ans[1][i].second,0);
		}
		if(!find(op+3,p,q,y))	swap(p,q);
		if(find(op+3,p,q,w)){
			ans[op].insert(ans[op].begin()+pal+1,{w,x});
			ans[nop][pal]={x,z},ans[nop].insert(ans[nop].begin()+pal+1,{x,y});
		}
		else{
			ans[op].insert(ans[op].begin()+pal+1,{w,x});
			ans[nop][pal]={x,y},ans[nop].insert(ans[nop].begin()+pal+1,{x,z});
		}
		add(op,x,w),add(nop,x,y),add(nop,x,z),erase(nop,y,z),vis[x]=0;
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int op=0;op<2;op++)	for(int i=1,x,y;i<n;i++)
		cin>>x>>y,edge[op][i]={x,y},add(op,x,y);
	dfs(n);
	for(int op=0;op<2;op++){
		for(pair<int,int> i:ans[op]){
			for(int j=1;j<n;j++) if(equ(i,edge[op][j])){
				cout<<j<<" ";
				break;
			}
		}
		cout<<endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3800kb

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

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

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:

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

result:

ok Correct, N = 7

Test #4:

score: -100
Memory 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: