QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#863816#9881. Diverge and ConvergeczcWA 1ms5992kbC++143.9kb2025-01-19 22:59:522025-01-19 22:59:53

Judging History

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

  • [2025-01-19 22:59:53]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5992kb
  • [2025-01-19 22:59:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
int id1[maxn][maxn],id2[maxn][maxn];
vector< pair<int,int> > e1,e2;
struct node{
	int a1,b1,a2,b2;
};
int deg1[maxn],deg2[maxn];
vector<int> G[maxn];
inline int dfs(int x,int fa,int to){
	if(x==to) return 1;
	for(auto y:G[x]){
		if(y==fa) continue;
		if(dfs(y,x,to)) return 1;
	}
	return 0;
}
int N;
inline int check(int u,int a,int b,vector<pair<int,int> > E){
	for(int i=1;i<=N;i++) G[i].clear();
	for(auto x:E) G[x.first].push_back(x.second),G[x.second].push_back(x.first);
	return dfs(b,u,a);
}
inline vector<node> solve(int n,vector<pair<int,int> > E1,vector<pair<int,int> > E2){
	if(n==1){
		vector<node> ret;
		return ret;
	}
//	cout<<"id:"<<n<<endl;
//	for(auto x:E1){
//		cout<<"czc:"<<x.first<<" "<<x.second<<endl;
//	}
//	for(auto x:E2){
//		cout<<"pyb:"<<x.first<<" "<<x.second<<endl;
//	}
	memset(deg1,0,sizeof(deg1));
	memset(deg2,0,sizeof(deg2));
	for(auto x:E1) deg1[x.first]++,deg1[x.second]++;
	for(auto x:E2) deg2[x.first]++,deg2[x.second]++;
	int u=0;
	for(int i=1;i<=N;i++){
		if(deg1[i] && deg2[i] && deg1[i]+deg2[i]<=3){
			u=i;
			break;
		}	
	}
//	cout<<u<<endl;
	if(deg1[u]==1 && deg2[u]==1){
//		cout<<"yes1\n";
		vector<pair<int,int>> ee1,ee2;
		int a=0,b=0;
		for(auto x:E1){
			if(x.first==u){
				a=x.second;
				continue;
			}
			if(x.second==u){
				a=x.first;
				continue;
			}
			ee1.push_back(x);
		}
		for(auto x:E2){
			if(x.first==u){
				b=x.second;
				continue;
			}
			if(x.second==u){
				b=x.first;
				continue;
			}
			ee2.push_back(x);
		}
		vector<node> ret=solve(n-1,ee1,ee2);
		vector<node> ret2;
		ret2.push_back({a,u,b,u});
		for(auto x:ret) ret2.push_back(x);
		return ret2;
	}
	if(deg1[u]==1 && deg2[u]==2){
		vector<pair<int,int>> ee1,ee2;
		int a=0,b=0,c=0;
		for(auto x:E1){
			if(x.first==u){
				a=x.second;
				continue;
			}
			if(x.second==u){
				a=x.first;
				continue;
			}
			ee1.push_back(x);
		}
		for(auto x:E2){
			if(x.first==u){
				if(!b) b=x.second;
				else c=x.second;
				continue;
			}
			if(x.second==u){
				if(!b) b=x.first;
				else c=x.first;
				continue;
			}
			ee2.push_back(x);
		}
		if(!check(u,a,b,E2)) swap(b,c);
		ee2.push_back({b,c});
		vector<node> ret=solve(n-1,ee1,ee2);
		vector<node> ret2;
//		cout<<b<<" "<<c<<endl;
		for(auto x:ret){
//			cout<<"??"<<x.a1<<" "<<x.b1<<" "<<x.a2<<" "<<x.b2<<endl;
			if((x.a2==b && x.b2==c) || (x.a2==c && x.b2==b)){
				int d=x.a1,g=x.b1;
				ret2.push_back({u,a,u,b});
				ret2.push_back({d,g,u,c});
			}
			else{
				ret2.push_back(x);
			}
		}
		return ret2;
	}
		vector<pair<int,int>> ee1,ee2;
		int a=0,b=0,c=0;
		for(auto x:E1){
			if(x.first==u){
				if(!b) b=x.second;
				else c=x.second;
				continue;
			}
			if(x.second==u){
				if(!b) b=x.first;
				else c=x.first;
				continue;
			}
			ee1.push_back(x);
		}
		for(auto x:E2){
			if(x.first==u){
				a=x.second;
				continue;
			}
			if(x.second==u){
				a=x.first;
				continue;
			}
			ee2.push_back(x);
		}
		if(!check(u,a,b,E1)) swap(b,c);
		ee2.push_back({b,c});
		vector<node> ret=solve(n-1,ee1,ee2);
		vector<node> ret2;
		for(auto x:ret){
			if((x.a1==b && x.b1==c) || (x.a1==c && x.b1==b)){
				int d=x.a2,g=x.b2;
				ret2.push_back({u,b,u,b});
				ret2.push_back({u,c,d,g});
			}
			else{
				ret2.push_back(x);
			}
		}
		return ret2;
}
int n;
int main(){
	scanf("%d",&n);
	N=n;
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		e1.push_back({u,v});
		id1[u][v]=id1[v][u]=i;
	}
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		e2.push_back({u,v});
		id2[u][v]=id2[v][u]=i;
	}
	vector<node> ans=solve(n,e1,e2);
	cout<<(int)ans.size()<<endl;
	for(int i=0;i<n-1;i++){
		printf("%d ",id1[ans[i].a1][ans[i].b1]);
	}
	putchar('\n');
	for(int i=0;i<n-1;i++){
		printf("%d ",id2[ans[i].a2][ans[i].b2]);
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5992kb

input:

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

output:

3
1 2 3 
1 3 2 

result:

wrong answer Wrong, Q is not permutation of (1,2,...,N-1)