BZOJ 3162 独钓寒江雪

树哈希+树形DP+组合计数

VFK题解里用的是直径的中心,然而我用的是重心,不过基本没区别。

所有独立集计数记f[i][0/1]表示i取或不取,子树内独立集个数。要求本质不同,那我们希望拿一个点r出来当根,尽量让以r为根的有根树形态唯一,即不存在树内一点u,使得u当根和r当根时结构一样。重心是一个不错的选择,因为一棵树重心至多两个。

对于重心有两个的情况大力分类讨论即可,前面的做法和重心唯一的做法一样。考虑重心唯一怎么做。

对于重心唯一的子树r,我们只需考虑子树内的同构问题,而不用考虑换根之后的同构问题(因为根是中心,不可能有上述的u点)。做树形DP。考虑子树的同构问题,对于m个结构相同的子树,每一个子树都有n种关于子树(即只考虑子树同构,不考虑上述u点)本质不同的独立集个数,答案就是:n 种物品,每种物品无限个,求取 m 个的组合数。给出结论:答案=C(n+m-1,m),至于怎么来的就去看VFK题解吧(甩锅233).

hash函数一定要弄得奇葩一点,要不然就会被莫名卡掉(好气)

给出AC的hash函数,事先将子树的hash值从小到大排序了:


    const int A = 233, B = 998244353;
    void make_hash(int x, int arrcnt)
    {
        hash[x] = 1;
        for(int i = 1; i <= arrcnt; i++)
            hash[x] = (((ll) hash[x] * A + arr[i]) ^ (arr[i] << 3)) % B;
    }
#include<cstdio>
#include<algorithm>
#define N 500005
#define MOD 1000000007
using namespace std;
namespace runzhe2000
{
    typedef long long ll;
    int read()
    {
        int r = 0; char c = getchar();
        for(; c < '0' || c > '9'; c = getchar());
        for(; c >='0' && c <='9'; r = r*10+c-'0', c = getchar());
        return r;
    }
    const int A = 233, B = 998244353;
    int inv[N<<1], invfac[N<<1];
    int n, ecnt, last[N], siz[N], root, root2, fa[N], hash[N], arr[N], f[N][2], ans;
    struct edge{int next, to;}e[N<<1];
    void addedge(int a, int b)
    {
        e[++ecnt] = (edge){last[a], b};
        last[a] = ecnt;
    }
    void num_init()
    {
        inv[1] = invfac[1] = 1;
        for(int i = 2, ii = N<<1; i < ii; i++)
        {
            inv[i] = (ll) (MOD - MOD / i) * inv[MOD % i] % MOD;
            invfac[i] = (ll) invfac[i-1] * inv[i] % MOD;
        }
    }
    int C(int a, int b)
    {
        if(a < b) return 0;
        int ret = invfac[b];
        for(int i = a-b+1; i <= a; i++) ret = (ll)ret * i % MOD;
        return ret;
    }
    void make_hash(int x, int arrcnt)
    {
        hash[x] = 1;
        for(int i = 1; i <= arrcnt; i++)
            hash[x] = (((ll) hash[x] * A + arr[i]) ^ (arr[i] << 3)) % B;
    }
    void dfs_siz(int x, int fath)
    {
        bool iscg = 1; siz[x] = 1;
        for(int i = last[x]; i; i = e[i].next)
        {
            int y = e[i].to;
            if(y == fath) continue;
            dfs_siz(y, x);
            siz[x] += siz[y];
            if(siz[y] > n/2) iscg = 0;
        }
        if(n - siz[x] > n/2) iscg = 0;
        if(iscg) root = x;
    }
    void dfs_init(int x)
    {
        siz[x] = 1; 
        for(int i = last[x]; i; i = e[i].next)
        {
            int y = e[i].to;
            if(y == fa[x]) continue;
            fa[y] = x;
            dfs_init(y); 
            siz[x] += siz[y];
        }
        int arrcnt = 0;
        for(int i = last[x]; i; i = e[i].next)
            if(e[i].to != fa[x]) arr[++arrcnt] = hash[e[i].to];
        sort(arr+1, arr+1+arrcnt);
        make_hash(x, arrcnt);
    }
    bool cmp_hash(int a, int b){return hash[a] < hash[b];}
    void dfs_dp(int x)
    {
        for(int i = last[x]; i; i = e[i].next)
        {
            int y = e[i].to;
            if(y == fa[x]) continue;
            dfs_dp(y);
        }
        int arrcnt = 0;
        for(int i = last[x]; i; i = e[i].next)
            if(e[i].to != fa[x]) arr[++arrcnt] = e[i].to;
        sort(arr+1, arr+1+arrcnt, cmp_hash);
        f[x][0] = f[x][1] = 1;
        int cnt = 0, curhash = -1, p;
        for(int i = 1; i <= arrcnt; i++)
        {
            int y = arr[i];
            if(hash[y] != curhash)
            {
                if(curhash != -1)
                {
                    f[x][1] = (ll) f[x][1] * C(f[p][0]+cnt-1, cnt) % MOD;
                    f[x][0] = (ll) f[x][0] * C(f[p][0]+f[p][1]+cnt-1, cnt) % MOD;
                }
                curhash = hash[y];
                cnt = 1; p = y;
            }
            else cnt++; 
        }
        if(curhash != -1)
        {
            f[x][1] = (ll) f[x][1] * C(f[p][0]+cnt-1, cnt) % MOD;
            f[x][0] = (ll) f[x][0] * C(f[p][0]+f[p][1]+cnt-1, cnt) % MOD;
        }
    }
    void judge()
    {
        int root2 = 0;
        for(int i = last[root]; i; i = e[i].next)
        {
            int y = e[i].to, iscg = 1;
            for(int j = last[y]; j && iscg; j = e[j].next)
            {
                int z = e[j].to; if(z == root)continue;
                if(siz[z] > n/2) iscg = 0;
            }
            if(n - siz[y] > n/2) iscg = 0;
            if(iscg){root2 = y; break;}
        }
        if(root2)
        {
            int arrcnt = 0;
            for(int i = last[root]; i; i = e[i].next)
            {
                int y = e[i].to;
                if(y == root2) continue;
                arr[++arrcnt] = hash[y];
            }
            sort(arr+1, arr+1+arrcnt);
            make_hash(root, arrcnt);
            if(hash[root] == hash[root2])
                ans = (f[root][1] + (ll)f[root2][0]*(1+f[root2][0])/2) % MOD;
            else ans = (f[root][0] + f[root][1]) % MOD;
        }
        else ans = (f[root][0] + f[root][1]) % MOD;
    }
    void main()
    {
        n = read();
        for(int i = 1, a, b; i < n; i++)
        {
            addedge(a = read(), b = read());
            addedge(b, a);
        }
        num_init();
        dfs_siz(1,0);
        dfs_init(root);
        dfs_dp(root);
        judge();
        printf("%d\n",ans);
    }
}
int main()
{
    runzhe2000::main();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值