CF 462D(Appleman and Tree-树形dp)
2017-12-10 04:10
405 查看
题意:一棵n个节点的树(根为0),树上有一些点是黑色的,你希望删除一个边的集合,使每个剩下的联通块都恰好只有1个黑节点。求方案数。
dp[x][0]表示对于节点i的子树的一个划分,x所在的联通块无黑节点
dp[x][1]表示对于节点i的子树的一个划分,x所在的联通块有黑节点
然后转移,考虑x的每个子树是否与x在同一连通块、
dp[x][0]表示对于节点i的子树的一个划分,x所在的联通块无黑节点
dp[x][1]表示对于节点i的子树的一个划分,x所在的联通块有黑节点
然后转移,考虑x的每个子树是否与x在同一连通块、
#include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForkD(i,k,n) for(int i=n;i>=k;i--) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (o<<1) #define Rson ((o<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,0x3f,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define MEMx(a,b) memset(a,b,sizeof(a)); #define INF (0x3f3f3f3f) #define F (1000000007) #define pb push_back #define mp make_pair #define fi first #define se second #define vi vector<int> #define pi pair<int,int> #define SI(a) ((a).size()) #define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans); #define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a <<endl; #define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000") #define ALL(x) (x).begin(),(x).end() #define gmax(a,b) a=max(a,b); #define gmin(a,b) a=min(a,b); typedef long long ll; typedef long double ld; typedef unsigned long long ull; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return ((a-b)%F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} inline int read() { int x=0,f=1; char ch=getchar(); while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();} while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();} return x*f; } #define MAXN (112345) int n,col[MAXN]; vi e[MAXN]; int fa[MAXN]; ll dp[MAXN][2]; ll pow2(ll a,ll b) { if (!b) return 1%F; if(b==1) return a%F; ll p=pow2(a,b/2); p=mul(p,p); if(b&1) p=p*a%F; return p; } ll inv(ll a){ return pow2(a,F-2); } void dfs(int x){ for(auto v:e[x]) dfs(v); if(col[x]) { dp[x][0]=0; dp[x][1]=1; for(auto v:e[x]) { dp[x][1]=dp[x][1]*(dp[v][0]+dp[v][1])%F; } }else { dp[x][1]=0; dp[x][0]=1; for(auto v:e[x]) { dp[x][0]=dp[x][0]*(dp[v][0]+dp[v][1])%F; } for(auto v:e[x]) { upd(dp[x][1], (ll)dp[x][0]*inv(dp[v][0]+dp[v][1])%F*dp[v][1]%F); } } } int main() { // freopen("D.in","r",stdin); // freopen(".out","w",stdout); n=read();fa[0]=0; For(i,n-1) { fa[i]=read(); e[fa[i]].pb(i); } Rep(i,n) col[i]=read(); dfs(0); cout<<dp[0][1]<<endl; return 0; }
相关文章推荐
- CF 461B - Appleman and Tree(树形DP)
- codeforces 462 d Appleman and Tree(树形dp)
- Appleman and Tree - CodeForces 461 B 树形dp
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- 【Codeforces Round 263 (Div 2)D】【树形DP】Appleman and Tree 树上割k个黑点为k块的方案数
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
- Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
- Codeforces Round #263 (Div. 1) B. Appleman and Tree( 树形DP )
- codeforces 461B B. Appleman and Tree(树形dp)
- Codeforces Round #263 Appleman and Tree(树形DP)
- 【CF 736C】Ostap and Tree,树形DP
- Codeforces 461B - Appleman and Tree 树状DP
- CF Bear and Tree Jumps树形DP
- Codeforces 461B - Appleman and Tree 树状DP
- 【CF739B】Alyona and a tree(树上差分,二分,树形DP)
- Ural_1018. Binary Apple Tree(树形DP)
- [Ccodeforces 736C] Ostap and Tree - 树形DP
- E. Ostap and Tree(树形dp)
- Codeforces Round #263 (Div. 1 - B) Appleman and Tree