UOJ 185【ZJOI2016】小星星

4 篇文章 0 订阅
1 篇文章 0 订阅

求点数相同的一个无向图与一棵树有多少种不同的映射,使得树的边集为图的边集的子集。

首先orz吨爷

可以说是因为吨爷才去学的子集卷积(vfleaking)==结果过不掉
这样写树形dp感觉还行吧(燃烧的内存),好像这几次利用单调性什么的,最后都没用到+卷积,只是用or卷积,以后会遇上吗?

O(2nn3) 纯净TLE版

#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define travel(x) for(int I=last[x],to;to=e[I].to,I;I=e[I].nxt)
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
    int f=1;char c;x=0;
    while(c=getchar(),c<48)if(c=='-')f=-1;
    do x=x*10+(c^48);
    while(c=getchar(),c>47);
    x*=f;
}
template<class T>void nt(T x){
    if(!x)return;
    nt(x/10);
    putchar(x%10+'0');
}
template<class T>void pt(T x){
    if(x<0)putchar('-'),x=-x;
    if(!x)putchar('0');
    else nt(x);
}
template<class T>void ptn(T x){
    pt(x);putchar('\n');
}
template<class T>void pts(T x){
    pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

const int maxn=18;
int n,m;
int b[maxn][maxn];

int last[maxn],ecnt;
struct Edge{
    int to,nxt;
}e[maxn<<1];
void ins(int u,int v){
    e[++ecnt]=(Edge){v,last[u]};
    last[u]=ecnt;
}
ll dp[maxn][maxn][1<<maxn];
ll dp2[1<<maxn];

void ormul(ll *a,ll *b,ll *c){
    static ll A[1<<maxn],B[1<<maxn];
    rep(i,0,1<<n)A[i]=a[i],B[i]=b[i];
    rep(i,0,n)rep(j,1,1<<n)
        if(j>>i&1){
            A[j]+=A[j^1<<i];
            B[j]+=B[j^1<<i];
        }
    rep(i,0,1<<n)c[i]=A[i]*B[i];
    rep(i,0,n)rep(j,1,1<<n)
        if(j>>i&1)c[j]-=c[j^1<<i];
}


int cnt=0;
void dfs(int x,int f){
    travel(x)if(to!=f)dfs(to,x);
    rep(i,0,n){
        dp[x][i][1<<i]=1;
        travel(x)if(to!=f){
            rep(k,0,1<<n)dp2[k]=0;
            rep(j,0,n)if(b[i][j])
                rep(k,0,1<<n)dp2[k]+=dp[to][j][k];
            ormul(dp[x][i],dp2,dp[x][i]);
        }
    }
}

int main(){
//  freopen("pro.in","r",stdin);
//  freopen("ex_star2.in","r",stdin);
//  freopen("chk.out","w",stdout);
    sc(n);sc(m);
    rep(i,0,m){
        int u,v;
        sc(u);sc(v);--u;--v;
        b[u][v]=b[v][u]=true;
    }
    rep(i,1,n){
        int u,v;
        sc(u);sc(v);--u;--v;
        ins(u,v);ins(v,u);
    }
    dfs(0,-1);
    ll ans=0;int all=(1<<n)-1;
    rep(i,0,n)ans+=dp[0][i][all];
    ptn(ans);
    return 0;
}

看了StilWell的代码之后发现:
小范围直接卷会快很多(甚至都可以直接AC了)
不仅仅只有vfk版卷积

有一个更通用的构造,FWT(Fast Walsh-Hadamard Transform)(zhuohan123,Picks)

tfxor(A)=(tfxor(A0+A1),tfxor(A0A1))utfxor(A)=(utfxor(A0+A12),utfxor(A0A12))

tfand(A)=(tfand(A0+A1),tfand(A1))utfand(A)=(utfand(A0A1),utfand(A1))

tfor(A)=(tfor(A0),tfor(A1+A0))utfor(A)=(utfor(A0),utfor(A1A0))

笔记:
考虑一位时,

xorC0=(A0+A1)×(B0+B1)+(A0A1)×(B0B1)2=A0×B0+A1×B1xorC1=(A0+A1)×(B0+B1)(A0A1)×(B0B1)2=A0×B1+A1×B0

andC0=(A0+A1)×(B0+B1)A1×B1=andC1=A1×B1

orC0=A0×B0orC1=(A0+A1)×(B0+B1)A0×B0=

就是容斥==
然后很多位的的时候再来个大容斥
这就是FWT,用来解决位运算的卷积的构造

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值