1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef unsigned long long LL; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const LL maxn=500005; LL Size[maxn],cnt=0,tot=0,Hash[maxn],Maxson[maxn],tmp[maxn],Dfn[maxn],First[maxn],a[maxn],Min=0x7fffffff/2,Min1,Min2,Hash1; vector<int>edges[maxn]; bool cmp1(const int& a,const int& b) { return Size[a]<Size[b]||(Size[a]==Size[b]&&Hash[a]<Hash[b]); } void Dfs1(int Now,int father) { Size[Now]=1; for(int i=0; i<edges[Now].size(); i++) { int Next=edges[Now][i]; if(Next==father)continue; Dfs1(Next,Now); Size[Now]+=Size[Next]; } cnt=0; for(int i=0; i<edges[Now].size(); i++) { int Next=edges[Now][i]; if(Next==father)continue; a[++cnt]=Next; } sort(a+1,a+cnt+1,cmp1); a[cnt+1]=father; for(int i=0; i<edges[Now].size(); i++)edges[Now][i]=a[i+1]; Hash[Now]=Size[Now]; for(int i=0; i<edges[Now].size(); i++) { int Next=edges[Now][i]; if(Next==father)continue; Hash[Now]=Hash[Now]*99995999^Hash[Next]; } } void Dfs2(int Now,int father) { Dfn[++tot]=Now; for(int i=0; i<edges[Now].size(); i++) { int Next=edges[Now][i]; if(Next==father)continue; Dfs2(Next,Now); } } int n; void AddEdge(int x,int y) { edges[x].push_back(y); } void Find_Core(int Now,int father) { Size[Now]=1; for(int i=0; i<edges[Now].size(); i++) { int Next=edges[Now][i]; if(Next==father)continue; Find_Core(Next,Now); Size[Now]+=Size[Next]; Maxson[Now]=max(Maxson[Now],Size[Next]); } Maxson[Now]=max(Maxson[Now],n-Size[Now]); if(Maxson[Now]<Min) { Min=Maxson[Now]; Min1=Now; Min2=0; } else if(Maxson[Now]==Min)Min2=Now; } void Clear() { memset(Maxson,0,sizeof(Maxson)); Min=0x7fffffff/2; Min1=Min2=0; for(int i=1; i<=n; i++)edges[i].clear(); cnt=0; memset(a,0,sizeof(a)); memset(Hash,0,sizeof(Hash)); } struct Compare { int x,y; bool operator < (const Compare& b) const { return x<b.x; } } cmp[maxn]; int main() { n=Get_Int(); for(int i=1; i<n; i++) { int x=Get_Int(),y=Get_Int(); AddEdge(x,y); AddEdge(y,x); } Find_Core(1,0); Dfs1(Min1,0); Dfs2(Min1,0); Hash1=Hash[Min1]; for(int i=1; i<=n; i++)First[i]=Dfn[i]; Clear(); for(int i=1; i<n; i++) { int x=Get_Int(),y=Get_Int(); AddEdge(x,y); AddEdge(y,x); } Find_Core(1,0); Dfs1(Min1,0); tot=0; Dfs2(Min1,0); if(Hash1==Hash[Min1]) { for(int i=1; i<=n; i++)cmp[i].x=First[i],cmp[i].y=Dfn[i]; sort(cmp+1,cmp+n+1); puts("YES"); for(int i=1; i<=n; i++)printf("%d ",cmp[i].y); } else if(Min2) { Dfs1(Min2,0); tot=0; Dfs2(Min2,0); if(Hash1==Hash[Min2]) { for(int i=1; i<=n; i++)cmp[i].x=First[i],cmp[i].y=Dfn[i]; sort(cmp+1,cmp+n+1); puts("YES"); for(int i=1; i<=n; i++)printf("%d ",cmp[i].y); } else puts("NO"); } else puts("NO"); return 0; }
|