BZOJ 3162 獨釣寒江雪(樹形DP)

題目鏈接:BZOJ 3162

題目大意:求一棵無根樹上本質不同的獨立集個數

題解:經典的求樹的獨立集個數可以DP來做,f[x][0/1]分別表示以x爲根的子樹中不選點x和選點x的獨立集個數,初始f[x][0]=f[x][1]=1,轉移的時候f[x][0]*=f[v][0]+f[v][1],f[x][1]*=f[v][0] /*v是x的子節點*/
這個題只需要考慮一下同構就可以了。判斷樹是否同構可以哈希。轉移時若一個節點x有v_1,v_2,…v_m m個同構的子樹,則f[x][1]*=C(m+f[v1][0]-1,m),即相當於f[v1][0]種物品,每種有無限個,從中取m個物品的本質不同方案數。同理f[x][0]*=C(m+f[v1][0]+f[v1][1]-1,m)。爲了方便可以選擇無根樹的重心作爲根來DP(如果有兩個重心就建一個虛擬節點作爲根,兩個重心連向它),以保證在任意一個點x,子樹外的部分與x的各子樹不會同構。

code(有參考hzwer大神(⊙_⊙))

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 500005
#define inf 1000000000
#define mod 1000000007
#define seed 9875321
using namespace std;
inline int read()
{
    char c=getchar(); int num=0,f=1;
    while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
    while (c<='9'&&c>='0') { num=num*10+c-'0'; c=getchar(); }
    return num*f;
}
struct edge{
    int to,ne;
}e[N<<1];
int n,root,tot=1,head[N],h[2],siz[N],sta[N],top,mn=inf;
long long inv[N],f[N][2],ans;
unsigned long long H[N];
inline bool cmp(int x,int y) { return H[x]>H[y]; }
inline void push(int x,int y) { e[++tot].to=y; e[tot].ne=head[x]; head[x]=tot; }
void pre()
{
    inv[1]=1; 
    for (int i=2;i<=n;i++) 
     inv[i]=(long long)inv[mod%i]*(mod-mod/i)%mod;
}
int C(int n,int m)
{
    long long ret=1; n%=mod;
    for (int i=1;i<=m;i++) ret=ret*(n-i+1)%mod*inv[i]%mod;
    return ret;
}
inline int color(int n,long long m) { return C(n+m-1,n); }
void dfs(int now,int pre)
{
    siz[now]=1; int tmp=0;
    for (int i=head[now];i;i=e[i].ne)
    {
        int v=e[i].to; if (v==pre) continue;
        dfs(v,now); siz[now]+=siz[v];
        tmp=max(tmp,siz[v]);
    }
    tmp=max(tmp,n-siz[now]);
    if (tmp<mn) mn=tmp,h[0]=now,h[1]=0;
     else if (tmp==mn) h[1]=now;
}
void dp(int now,int pre)
{
    f[now][0]=f[now][1]=1;
    for (int i=head[now];i;i=e[i].ne)
    {
        int v=e[i].to; if (v==pre) continue;
        dp(v,now); 
    }
    int j; top=0;
    for (int i=head[now];i;i=e[i].ne)
    {
        int v=e[i].to; if (v==pre) continue;
        sta[++top]=v;
    }
    sort(sta+1,sta+top+1,cmp);
    for (int i=1;i<=top;i=j)
    {
        for (j=i+1;j<=top&&H[sta[i]]==H[sta[j]];j++);
        f[now][0]=f[now][0]*color(j-i,f[sta[i]][0]+f[sta[i]][1])%mod;
        f[now][1]=f[now][1]*color(j-i,f[sta[i]][0])%mod;
    }
    H[now]=233;
    for (int i=1;i<=top;i++) (((H[now]*=seed)+=H[sta[i]])^=H[sta[i]])+=H[sta[i]];
    /*樹哈希*/
}
int main()
{
    n=read(); pre();
    for (int i=1;i<n;i++) 
    {
        int x=read(),y=read();
        push(x,y); push(y,x);
    }
    dfs(1,0);
    if (h[1])
    {
        for (int i=head[h[0]];i;i=e[i].ne)
         if (e[i].to==h[1]) e[i].to=e[i^1].to=root=n+1;
        push(n+1,h[0]); push(n+1,h[1]);
    }
    else root=h[0];
    dp(root,0);
    if (!h[1]) ans=(f[root][0]+f[root][1])%mod;
    else
    {
        int x=h[0],y=h[1];
        if (H[x]==H[y]) ans=(f[x][0]*f[y][1]%mod+color(2,f[x][0]))%mod;
         else ans=(f[x][0]*f[y][0]%mod+f[x][1]*f[y][0]%mod+f[x][0]*f[y][1]%mod)%mod;
    }
    printf("%lld",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章