QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#863821 | #9881. Diverge and Converge | czc | RE | 0ms | 8048kb | C++14 | 3.9kb | 2025-01-19 23:07:38 | 2025-01-19 23:07:39 |
Judging History
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);
ee1.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: 100
Accepted
time: 0ms
memory: 5836kb
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: 7972kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: 0
Accepted
time: 0ms
memory: 8048kb
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 5 2 6 6 4 1 5 2 3
result:
ok Correct, N = 7
Test #4:
score: -100
Runtime Error
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...