QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#865730 | #9881. Diverge and Converge | shung | WA | 0ms | 4864kb | C++14 | 2.4kb | 2025-01-21 21:31:02 | 2025-01-21 21:31:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=10005;
int n,deg1[N],deg2[N],ans1[N],ans2[N],now,son[N][2],v[N],tot;
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;
}
}
inline void work(int x,int y){
if(son[x][0]){
work(son[x][0],v[x]);
work(son[x][1],y);
}
else if(son[y][0]){
work(v[y],son[y][0]);
work(x,son[y][1]);
}
else{
++now;
ans1[now]=x,ans2[now]=y;
}
}
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];
}
tot=n+n;
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;
int j=(*i1).second;
mp1[y].erase(x);
--deg1[y];
auto i2=mp2[x].begin();
y=(*i2).first;
int k=(*i2).second;
mp2[y].erase(x);
--deg2[y];
work(j,k);
}
else{
if(deg1[x]==1){
auto i1=mp1[x].begin();
int a=(*i1).first;
mp1[a].erase(x);
--deg1[a];
int j=(*i1).second;
auto i2=mp2[x].begin();
int b=(*i2).first,B=(*i2).second;
++i2;
int c=(*i2).first,C=(*i2).second;
dfs2(b,x);
mp2[b].erase(x),mp2[c].erase(x);
mp2[b][c]=mp2[c][b]=++tot;
memset(bo,0,sizeof(bo));
if(bo[a]){
son[tot][0]=B,son[tot][1]=C,v[tot]=j;
}
else{
son[tot][0]=C,son[tot][1]=B,v[tot]=j;
}
}
else{
auto i2=mp2[x].begin();
int a=(*i2).first;
mp2[a].erase(x);
--deg2[a];
int j=(*i2).second;
auto i1=mp1[x].begin();
int b=(*i1).first,B=(*i1).second;
++i1;
int c=(*i1).first,C=(*i1).second;
dfs1(b,x);
mp1[b].erase(x),mp1[c].erase(x);
mp1[b][c]=mp1[c][b]=++tot;
memset(bo,0,sizeof(bo));
if(bo[a]){
son[tot][0]=B,son[tot][1]=C,v[tot]=j;
}
else{
son[tot][0]=C,son[tot][1]=B,v[tot]=j;
}
}
}
}
for(int i=1;i<n;++i)printf("%d ",ans1[i]);
printf("\n");
for(int i=1;i<n;++i)printf("%d ",ans2[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 4864kb
input:
4 1 2 2 3 3 4 1 2 2 4 2 3
output:
1 2 3 1 2 3
result:
wrong answer Wrong, N = 4, not forming a tree