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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 output

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?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.

Output

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

Sample test(s) 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,? 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美特级黄色片 | 亚洲一区二区三区播放 | 白丝开裆喷水 | 色综合久久88色综合天天免费 | 九九九九精品九九九九 | 丁香一区二区 | 国产在线天堂 | 欧美成人性生活片 | 日韩啪啪片 | av资源网址| 人体内射精一区二区三区 | 亚洲天堂女人 | 国产亚洲精品久久久久久777 | h狠狠躁死你h高h | 成人午夜精品视频 | 91xxxxx| 黄页免费网站 | 特黄一级大片 | 久久国产人妻一区二区免色戒电影 | 高清一区二区 | 国产无套丰满白嫩对白 | 精品视频不卡 | 午夜精品久久久久久久99热浪潮 | 碰碰久久 | 久久久黄色片 | 成人日批视频 | 国产成人免费在线视频 | 国产精品一区二区三区在线播放 | 免费在线播放黄色片 | 欧美特级特黄aaaaaa在线看 | 色妹av | 日本a在线 | 免费久久一级欧美特大黄 | 色网站在线观看 | 国产黄色激情视频 | 风流少妇按摩来高潮 | 黄色网页免费在线观看 | 久久国产成人精品国产成人亚洲 | 一个人看的视频www 色就是色网站 | 一级片自拍 | 欧美日韩综合一区二区三区 | 999久久久久 | 一区二区免费 | 午夜精品导航 | 98久久久 | 日韩a视频| 欧美精品免费播放 | 免费黄色网址在线 | 一区二区三区在线观看av | 日韩一卡二卡在线 | 怡红院av亚洲一区二区三区h | 日本九九视频 | 免费看黄色三级 | 夜夜爽av福利精品导航 | 国产女教师bbwbbwbbw | 日本黄网站在线观看 | 久久涩涩| 熟女性饥渴一区二区三区 | 国产又粗又长又硬免费视频 | 床上激情网站 | 精精国产xxxx视频在线 | 欧美综合视频在线 | 国产成人综合视频 | 欧美激情在线免费 | 久久久久久久蜜桃 | 日韩久久久精品 | 日批视频免费看 | 深夜视频免费在线观看 | 亚洲精品国产精品国自 | 自拍视频在线 | 综合天天色 | 奇米色影视 | 六月婷婷激情网 | 日韩激情在线观看 | 乱亲女h秽乱长久久久 | 中文自拍| 亚洲国产婷婷香蕉久久久久久99 | 91在线播放国产 | 亚洲人成在线观看 | 久草国产精品 | www.亚洲欧美 | 亚洲第一男人天堂 | 国产一区av在线 | 国产亚洲av综合人人澡精品 | 亚洲欧美综合自拍 | 日韩网站视频 | 久久国产精品综合 | 国产精品色片 | 中文字幕一区二区人妻电影丶 | 一区二区三区欧美精品 | 中文字幕乱码人妻二区三区 | 欧美日韩欧美日韩在线观看视频 | 久草小说| 日本五十路 | 国产亚洲激情 | 午夜在线观看视频18 | 禁漫天堂黄漫画无遮挡观看 | 国产视频黄 | 波多野结衣福利 |