日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Appleman and Tree CodeForces - 461B(树形dp)

發(fā)布時間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Appleman and Tree CodeForces - 461B(树形dp) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.

Consider a set consisting of k (0?≤?k?<?n) edges of Appleman’s tree. If Appleman deletes these edges from the tree, then it will split into (k?+?1) parts. Note, that each part will be a tree with colored vertices.

Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109?+?7).

Input
The first line contains an integer n (2??≤?n?≤?105) — the number of tree vertices.

The second line contains the description of the tree: n?-?1 integers p 0,?p 1,?…,?p n?-?2 (0?≤?p i?≤?i). Where p i means that there is an edge connecting vertex (i?+?1) of the tree and vertex p i. Consider tree vertices are numbered from 0 to n?-?1.

The third line contains the description of the colors of the vertices: n integers x 0,?x 1,?…,?x n?-?1 ( x i is either 0 or 1). If x i is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.

Output
Output a single integer — the number of ways to split the tree modulo 1000000007 (109?+?7).

Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
dp數(shù)組的每一種狀態(tài)代表著什么,并且每一種狀態(tài)之間如何轉(zhuǎn)換是解決dp問題的關(guān)鍵之處。
思路:
dp[i][0]代表著以i為父節(jié)點的樹,沒有黑點的狀態(tài)數(shù)目。
dp[i][1]代表著以i為父節(jié)點的數(shù),有1個黑點的狀態(tài)數(shù)目。
狀態(tài)轉(zhuǎn)移方程:
①如果當(dāng)前節(jié)點是黑色,父親節(jié)點也是黑色,那么只能斷開,只對dp[u][1]有貢獻(xiàn)。
②如果當(dāng)前節(jié)點是黑色,父親節(jié)點是白色,可以與父親節(jié)點合并,也可以與父親節(jié)點斷開,對dp[u][0]和dp[u][1]同時有貢獻(xiàn)。
③如果當(dāng)前節(jié)點是白色,父節(jié)點是黑色,只能合并,對dp[u][1]有貢獻(xiàn)。
④如果當(dāng)前節(jié)點是白色,父親節(jié)點是白色,只能合并,對dp[u][0]有貢獻(xiàn)。
最終輸出dp[0][1]就可以。
代碼如下:

#include<bits/stdc++.h> #define ll long long #define mod 1000000007 using namespace std;const int maxx=1e5+100; struct edge{int to,next; }e[maxx<<1]; ll dp[maxx][2]; int head[maxx<<1]; int col[maxx]; int n,tot;inline void init() {memset(head,-1,sizeof(head));tot=0; } inline void add(int u,int v) {e[tot].next=head[u],e[tot].to=v,head[u]=tot++; } inline void dfs(int u,int f) {if(col[u]) dp[u][1]=1,dp[u][0]=0;else dp[u][0]=1,dp[u][1]=0;for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;if(to==f) continue;dfs(to,u);ll t1=dp[u][1],t0=dp[u][0];dp[u][1]=(t1*dp[to][0])%mod+(t0*dp[to][1])%mod+(t1*dp[to][1])%mod;dp[u][0]=(t0*dp[to][1])%mod+(t0*dp[to][0])%mod;dp[u][1]%=mod;dp[u][0]%=mod;} } int main() {scanf("%d",&n);init();int x;for(int i=1;i<n;i++){scanf("%d",&x);add(i,x);add(x,i);}for(int i=0;i<n;i++) scanf("%d",&col[i]);dfs(0,-1);printf("%d\n",dp[0][1]);return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Appleman and Tree CodeForces - 461B(树形dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。