BZOJ4455: [Zjoi2016]小星星(容斥)

传送门

题解:
我们能用 O(n2|S|) O ( n 2 | S | ) 的时间统计出所有点都是颜色集合 S S 中的一个颜色的方案树。

然后2n枚举 S S 做容斥即可。 时间复杂度O(3nn2)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int RLEN=1<<18|1;
inline char nc() {
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob) ? -1 : *ib++;
}
inline int rd() {
    char ch=nc(); int i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
    return i*f;
}

const int N=20;
int n, m, G[N][N], bin[N], a[N], tot;
LL f[N][N];
vector <int> edge[N];
inline void dfs(int x,int fa,int sta) {
    for(int e=edge[x].size()-1;e>=0;e--) {
        int v=edge[x][e]; if(v==fa) continue;
        dfs(v,x,sta);
    }
    for(int now=1;now<=tot;++now) {
        LL rs=1;
        for(int e=edge[x].size()-1;e>=0;e--) {
            int v=edge[x][e]; if(v==fa) continue;
            LL sum=0;
            for(int j=1;j<=tot;j++)
                if(G[a[now]][a[j]]) sum+=f[v][a[j]];
            rs=rs*sum;
        }
        f[x][a[now]]=rs;
    }
}
inline LL calc(int sta) {
    memset(f,0,sizeof(f));
    tot=0;
    for(int i=0;i<n;i++)
        if((sta>>i)&1) a[++tot]=i;
    dfs(0,0,sta); LL rs=0;
    for(int i=0;i<n;i++) rs+=f[0][i];
    return rs;
}
int main() {
    n=rd(); m=rd();
    for(int i=1;i<=m;i++) {
        int x=rd()-1, y=rd()-1;
        G[x][y]=G[y][x]=1;
    }
    for(int i=1;i<n;i++) {
        int x=rd()-1, y=rd()-1;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    for(int i=0;i<=n;i++) bin[i]=1<<i;
    long long ans=0; 
    for(int i=0;i<bin[n];i++)
        ans+=calc(i)*((__builtin_popcount(i)&1) ? 1 : -1);
    cout<<abs(ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值