[关闭]
@ysner 2018-06-09T22:43:12.000000Z 字数 2539 阅读 1845

在此处输入标题

未分类


  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstdio>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<unordered_set>
  8. #define ll unsigned long long
  9. #define re register
  10. #define il inline
  11. #define fp(i,a,b) for(re int i=a;i<=b;i++)
  12. #define fq(i,a,b) for(re int i=a;i>=b;i--)
  13. using namespace std;
  14. const int N=5e5+10,B1=157,B2=233,B3=359,p=1e9+7;
  15. unordered_set<ll>Q;
  16. int h[N],cnt;
  17. ll Hash[N];
  18. struct Edge{int to,nxt;}e[N<<1];
  19. il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
  20. il ll gi()
  21. {
  22. re ll x=0,t=1;
  23. re char ch=getchar();
  24. while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  25. if(ch=='-') t=-1,ch=getchar();
  26. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  27. return x*t;
  28. }
  29. il void dfs(re int u,re int fa)
  30. {
  31. sz[u]=1;re ll sum=0;
  32. for(re int i=h[u];i+1;i=e[i].nxt)
  33. {
  34. re int v=e[i].to;
  35. if(v==fa) continue;
  36. dfs(v,u);
  37. sz[u]+=sz[v];
  38. Hash[u]=Hash[u]^Hash[v]+17;
  39. }
  40. Hash[x]+=sz[u]*p+1;
  41. }
  42. il void dfs1(re int u,re int fa)
  43. {
  44. Q.insert(Hash[u]);
  45. for(re int i=h[u];i+1;i=e[i].nxt)
  46. {
  47. re int v=e[i].to;
  48. if(v==fa) continue;
  49. re ll tmp=(Hash[u]-sz[u]*p-1)^(Hash[y]+17);
  50. tmp+=(n-sz[v])*p+1;
  51. Hash[v]-=sz[v]*p+1;
  52. Hash[v]^=tmp+17;
  53. Hash[v]+=n*p+1;
  54. sz[c]=n;
  55. dfs1(v,u);
  56. }
  57. }
  58. int main()
  59. {
  60. memset(h,-1,sizeof(h));
  61. n=gi();
  62. fp(i,1,n-1)
  63. {
  64. re int u=gi(),v=gi();
  65. add(u,v);add(v,u);
  66. }
  67. dfs(1,0);dfs1(1,0);
  68. memset(h,-1,sizeof(h));memset(Hash,0,sizeof(Hash));
  69. return 0;
  70. }
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<unordered_set>
  6. #define ll long long
  7. #define N 200005
  8. using namespace std;
  9. const ll p=1e9+7;
  10. unordered_set<ll>Q;
  11. ll Hash[N];
  12. int n,D[N],si[N],Ans=1e9;
  13. int TOT,LA[N],NE[N],EN[N];
  14. void ADD(int x,int y)
  15. {
  16. TOT++;
  17. EN[TOT]=y;
  18. NE[TOT]=LA[x];
  19. LA[x]=TOT;
  20. }
  21. void Ghash(int x,int f)
  22. {
  23. int i,y;si[x]=1;
  24. for(i=LA[x];i;i=NE[i])
  25. {
  26. y=EN[i];if(y==f)continue;
  27. Ghash(y,x);si[x]+=si[y];
  28. Hash[x]=Hash[x]^Hash[y]+17;
  29. }
  30. Hash[x]+=si[x]*p+1;
  31. }
  32. void DFS1(int x,int f)
  33. {
  34. int i,y;Q.insert(Hash[x]);
  35. for(i=LA[x];i;i=NE[i])
  36. {
  37. y=EN[i];if(y==f)continue;
  38. ll tmp=(Hash[x]-si[x]*p-1)^(Hash[y]+17);
  39. tmp+=(n-si[y])*p+1;
  40. Hash[y]-=si[y]*p+1;
  41. Hash[y]^=tmp+17;
  42. Hash[y]+=n*p+1;
  43. si[y]=n;DFS1(y,x);
  44. }
  45. }
  46. void DFS2(int x,int f)
  47. {
  48. int i,y;
  49. for(i=LA[x];i;i=NE[i])
  50. {
  51. y=EN[i];if(y==f)continue;
  52. if(D[y]>1)
  53. {
  54. ll tmp=(Hash[x]-si[x]*p-1)^(Hash[y]+17);
  55. tmp+=(si[x]-si[y])*p+1;
  56. Hash[y]-=si[y]*p+1;
  57. Hash[y]^=tmp+17;
  58. Hash[y]+=si[x]*p+1;
  59. si[y]=si[x];DFS2(y,x);
  60. }
  61. else
  62. {
  63. ll tmp=(Hash[x]-si[x]*p-1)^(Hash[y]+17);
  64. tmp+=(si[x]-si[y])*p+1;
  65. if(Q.count(tmp))Ans=min(Ans,y);
  66. }
  67. }
  68. }
  69. int main()
  70. {
  71. int i,j,k,x,y;
  72. scanf("%d",&n);
  73. for(i=1;i<n;i++)
  74. {
  75. scanf("%d%d",&x,&y);
  76. ADD(x,y);ADD(y,x);
  77. }
  78. Ghash(1,0);DFS1(1,0);TOT=0;
  79. memset(LA,0,sizeof(LA));
  80. memset(Hash,0,sizeof(Hash));
  81. for(i=1;i<=n;i++)
  82. {
  83. scanf("%d%d",&x,&y);
  84. D[x]++;D[y]++;
  85. ADD(x,y);ADD(y,x);
  86. }
  87. for(x=1;x<=n;x++)if(D[x]>1)break;
  88. Ghash(x,0);DFS2(x,0);
  89. printf("%d",Ans);
  90. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注