1. 程式人生 > >[BJOI2015]樹的同構

[BJOI2015]樹的同構

std .com edge string ext ID 單獨 ner 相同

https://zybuluo.com/ysner/note/1176508

題面

給出各種形態的樹,問哪些樹互為重構樹?

  • \(n\leq50\)

    解析

    \(method\ 1\)

    一開始沒註意到不論樹有沒有根,都要以樹的重心為根,根的不同可以改變樹的形態,如一棵樹變成一條鏈之類。
    樹的重心的要求是使子樹 最大規模 最小
    顯然使用樹哈希。
    \[Hash[x]=\sum_{異或和}(Hash[son_{1..k}]+Base1)*(sz[x]+Base2)+deep[x]*Base3\]
    看起來這式子很容易乘爆,我們可以模一個\(2^n\)(自然溢出也是同一原理),以減少對 大小在模數範圍以內 的二進制位的影響。
    最後再註意一下找完\(root\)

    後要重新統計子樹大小
    但在\(bzoj\)上死都過不了,很想蒯數據

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #define ll long long
    #define re register
    #define il inline
    #define fp(i,a,b) for(re int i=a;i<=b;i++)
    #define fq(i,a,b) for(re int i=a;i>=b;i--)
    using namespace std;
    const int N=105,mod1=498353,mod2=412817,mod=1<<30;
    int n,h[N],cnt,m,B1=3,B2=7,B3=11,sz[N],vis1[500000],vis2[500000],dp[N],root;
    ll Hash[N];
    struct Edge{int to,next;}e[N<<1];
    il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
    il void dfs(re int u,re int fa,re int deep)
    {
      //printf("%d %d %d\n",u,fa,deep);
      re ll sum=0;sz[u]=1;
      for(re int i=h[u];i+1;i=e[i].next)
    {
      re int v=e[i].to;
      if(v==fa) continue;
      dfs(v,u,deep+1);//printf("%d\n",Hash[v]);
      sum^=Hash[v];
      sz[u]+=sz[v];
    }
      Hash[u]^=((sum+B1)*(sz[u]+B2)*(deep+B3));
      Hash[u]%=mod;
      //printf("%lld %d\n",Hash[u],u);
    }
    il void getroot(re int u,re int fa)
    {
      sz[u]=1;
      for(re int i=h[u];i+1;i=e[i].next)
    {
      re int v=e[i].to;
      if(v==fa) continue;
      getroot(v,u);
      sz[u]+=sz[v];
      dp[u]=max(dp[u],sz[v]);
    }
      dp[u]=max(dp[u],n-dp[u]);
      if(dp[u]<dp[root]) root=u;
      else if(dp[u]==dp[root]&&u<root) root=u;
    }
    int main()
    {
      m=gi();
      fp(o,1,m)
    {
      memset(h,-1,sizeof(h));cnt=0;memset(Hash,0,sizeof(Hash));memset(dp,0,sizeof(dp));dp[0]=1e9;memset(sz,0,sizeof(sz));
      n=gi();root=0;
      fp(i,1,n)
    {
      re int v=gi();
      if(v) add(i,v),add(v,i);
    }
      getroot(1,0);//printf("%d %d\n",o,root);
      dfs(root,0,1);//printf("%d %lld\n",o,Hash[1]);
      if(vis1[Hash[root]%mod1]&&vis2[Hash[root]%mod2]) printf("%d\n",vis1[Hash[root]%mod1]);
      else printf("%d\n",vis1[Hash[root]%mod1]=vis2[Hash[root]%mod2]=o);
    }
      return 0;
    }

    \(method\ 2\)

    在對\(sum[v]\)進行排序後,
    哈希方程變為這樣(\(u\)為當前節點,\(v\)為子節點,\(p[i]\)為質數表)
    \[sum[u]=\sum sum[v]*p[i](u\in \{v\})\]
    當然當前節點也算單獨一顆\(size=1\)的子樹,要不然葉節點怎麽辦。。。
    因該式不考慮諸如深度、以該節點為根的子樹等因素,我們就要對每個點為根的情況都進行\(Hash\)值計算,最後排序以後比較是否完全相同即可。

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #define ll long long
    #define re register
    #define il inline
    #define fp(i,a,b) for(re int i=a;i<=b;i++)
    #define fq(i,a,b) for(re int i=a;i>=b;i--)
    using namespace std;
    const int N=105;
    int n,h[N],cnt,m,p[55];
    ll Hash[N][N],dp[N];
    struct Edge{int to,next;}e[N<<1];
    il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
    il ll gi() 
    {
      re ll x=0,t=1;
      re char ch=getchar();
      while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘) ch=getchar();
      if(ch==‘-‘) t=-1,ch=getchar();
      while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-48,ch=getchar();
      return x*t;
    }
    il void wri(re int x)
    {
      if(x<0) putchar(‘-‘),x=-x;
      if(x>9) wri(x/10);
      putchar(x%10+‘0‘);
    }
    il void dfs(re int u,re int fa)
    {
      re ll top=0,s[55];s[++top]=1;
      for(re int i=h[u];i+1;i=e[i].next)
    {
      re int v=e[i].to;
      if(v==fa) continue;
      dfs(v,u);
      s[++top]=dp[v];
    }
      dp[u]=0;sort(s+1,s+1+top);
      fp(i,1,top) dp[u]+=s[i]*p[i];
    }
    il void Pre()
    {
      re int tot=0;
      fp(i,41,300)
    {
      re int flag=1;
      fp(j,2,sqrt(i)) if(i%j==0) {flag=0;break;}
      if(flag) p[++tot]=i;
      if(tot>50) break;
    }
    }
    int main()
    {
      Pre();
      m=gi();
      fp(o,1,m)
    {
      memset(h,-1,sizeof(h));cnt=0;memset(dp,0,sizeof(dp));
      n=gi();
      fp(i,1,n)
    {
      re int v=gi();
      if(v) add(i,v),add(v,i);
    }
      fp(i,1,n) dfs(i,0),Hash[o][i]=dp[i];
      sort(Hash[o]+1,Hash[o]+1+n);
      //fp(i,1,n) printf("%lld ",Hash[o][i]);puts("");
      fp(i,1,o)
    {
      re int flag=1;
          fp(j,1,n) if(Hash[o][j]!=Hash[i][j])
        {
          //printf("%d %d %lld\n",i,j,Hash[i][j]);
          flag=0;break;
        }
      if(flag) {printf("%d\n",i);break;}
    }
    }
      return 0;
    }

[BJOI2015]樹的同構