Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
題目鏈接
?
D. Appleman and Tree
time limit per test :2 seconds memory limit per test:?256 megabytes input :standard input output:standard outputAppleman 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).
InputThe 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?p0,?p1,?...,?pn?-?2?(0?≤?pi?≤?i). Where?pi?means that there is an edge connecting vertex?(i?+?1)?of the tree and vertex?pi. Consider tree vertices are numbered from?0?to?n?-?1.
The third line contains the description of the colors of the vertices:?n?integers?x0,?x1,?...,?xn?-?1?(xi?is either?0?or?1). If?xi?is equal to?1, vertex?i?is colored black. Otherwise, vertex?i?is colored white.
OutputOutput a single integer — the number of ways to split the tree modulo?1000000007?(109?+?7).
Sample test(s) input 30 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,? dp[i][0]代表到 i 這個點它所在的子樹只有一個黑點的情況,dp[i][0] 包含i節點的這部分沒有黑點的情況數。
對于每個節點 i,計算到它的一個子樹(根節點u) (設連接的邊為edge)的時候,dp[i][0] 為dp[i][0] * dp[u][1] + dp[i][0] * dp[u][0], 已處理完的一定要取dp[i][0], 如果取edge 則子樹取dp[u][0],如果不取edge, 則子樹取dp[u][1].
dp[i][1] 為 dp[i][1] *(dp[u][0] + dp[u][1]) + dp[i][0] *dp[u][1] , 如果處理完的取dp[i][1],edge取的話為dp[u][0], 不取的話為dp[u][1]; 如果處理完的取dp[i][0], edge一定要取且要乘以dp[u][1] ?(ps: dp[u][0] 不能要,如果要的話 u點的部分會出現不含黑點的情況)
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #define mod 1000000007 5 6 using namespace std ; 7 8 struct node 9 { 10 int u ; 11 int v ; 12 int next ; 13 }p[100010]; 14 int cnt,head[100010],color[100010] ; 15 long long dp[100010][2] ; 16 17 void addedge(int u,int v) 18 { 19 p[cnt].u = u ; 20 p[cnt].v = v ; 21 p[cnt].next = head[u] ; 22 head[u] = cnt ++ ; 23 } 24 void DFS(int u) 25 { 26 dp[u][color[u]] = 1 ; 27 for(int i = head[u] ; i+1 ; i = p[i].next) 28 { 29 int v = p[i].v ; 30 DFS(v) ; 31 dp[u][1] = ((dp[u][1] * dp[v][0]) % mod + (dp[u][1] * dp[v][1]) % mod + (dp[u][0] * dp[v][1]) % mod) % mod ; 32 dp[u][0] = ((dp[u][0] * dp[v][0]) % mod + (dp[u][0] * dp[v][1]) % mod) % mod ; 33 } 34 } 35 int main() 36 { 37 int n ,a; 38 while(~scanf("%d",&n)) 39 { 40 cnt = 0 ; 41 memset(head,-1,sizeof(head)) ; 42 memset(dp,0,sizeof(dp)) ; 43 for(int i = 1 ; i < n ; i++) 44 { 45 scanf("%d",&a) ; 46 addedge(a,i) ; 47 } 48 for(int i = 0 ; i < n ; i++) 49 scanf("%d",&color[i]) ; 50 DFS(0) ; 51 printf("%I64d\n",dp[0][1]) ; 52 } 53 return 0 ; 54 } View Code?
轉載于:https://www.cnblogs.com/luyingfeng/p/3939754.html
總結
以上是生活随笔為你收集整理的Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvc:default-servlet-
- 下一篇: [Swift]LeetCode916.单