codeforces 酱游记

  Codeforces Round #263 Div.1:

  B. Appleman and Tree

  题目大意:给一棵树,每个点可能是黑色或白色。求有多少种方案使得这棵树被分成k份,每份有且仅有一个黑点。

  一看就知道是树形dp,可是不会做...题解思路很巧妙,很有借鉴意义。用dp[v][0]表示当前点及与它同一块的点没有黑点,dp[v][1]表示当前点及与它同一块的点有且仅有一个黑点。然后就是dp了。dp时有一个技巧:当前点为白色,而我们要求dp[v][1]时,我们必须只连接一个dp[child[i]][1]。我们可以直接用dp[v][0]来得到这时候的dp[v][1]。这样做既简洁又方便。实在是很神奇!

 1 //7697563     2014-09-07 04:12:28     moxiaomo     B - Appleman and Tree     GNU C++     Accepted     31 ms     8200 KB
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 typedef long long LL;
 5 const int maxn=100001;
 6 const LL MOD=1000000007;
 7
 8 int pos,lin[maxn],ta[maxn],sd[maxn];
 9 inline void biu(int s,int t) { ++pos; lin[pos]=ta[s]; ta[s]=pos; sd[pos]=t; }
10
11 int n;
12 int color[maxn];
13 LL dp[maxn][2];
14 void dfs(int v)
15 {
16     dp[v][color[v]]=1;
17     for (int i=ta[v];i;i=lin[i])
18     {
19         dfs(sd[i]);
20         if (color[v]==1)
21         {
22             //dp[v][0]=0;
23             dp[v][1]=dp[v][1]*(dp[sd[i]][0]+dp[sd[i]][1])%MOD;
24         }
25         else
26         {
27             dp[v][1]=dp[v][1]*(dp[sd[i]][0]+dp[sd[i]][1])%MOD;
28             dp[v][1]=(dp[v][1]+dp[v][0]*dp[sd[i]][1])%MOD;
29             dp[v][0]=dp[v][0]*(dp[sd[i]][0]+dp[sd[i]][1])%MOD;//--
30         }
31     }
32 }
33 int main()
34 {
35     scanf("%d",&n);
36     for (int i=0,p;i<n-1;i++) scanf("%d",&p),biu(p,i+1);
37     for (int i=0;i<n;i++) scanf("%d",color+i);
38     dfs(0);
39     cout<<dp[0][1]<<endl;
40 }

B. Appleman and Tree

时间: 2024-08-03 11:16:35

codeforces 酱游记的相关文章

网页游戏

网页游戏_百度百科http://baike.baidu.com/link?url=__mxqeSPiqAO_TGydqkd7M8BGmboUrei_rqfDzv59uOS9GxVe1UdbsClJgg2l5qCuFy4qzFWgaQo9GeZeaBpeiTo2npMjlMvFD6Wb-QNY8D1iT_phwtSVuq1RFOUAocv 页游 即 网页游戏 . 网页游戏又称Web游戏,无端网游,简称页游.是基于Web浏览器的网络在线多人互动游戏,无需下载客户端,只需打开IE网页,10秒钟即可进入

Codeforces Round #513 游记

Codeforces Round #513 游记 A - Phone Numbers 题目大意: 电话号码是8开头的\(1\)位数字.告诉你\(n(n\le100)\)个数字,每个数字至多使用一次.问最多能凑出多少个电话号码. 思路: 统计8出现的次数,如果有多余的8不能作为开头,那么就将其放到后面去 源代码: #include<cstdio> #include<cctype> #include<algorithm> inline int getint() { regi

Codeforces Round #500 (Div. 2) 游记

A Piles With Stones 题意 有\(N\)堆石子,任意个人.每个人可以把一堆石子中的一个石子移动到另一堆,或者是拿走一堆石子.现在给你石子一开始的情况与这些人进行操作后的情况,问是否合法. 思路 此题看上去不简单,但是你可能在几秒内想出一个结论,那就是:无论这些人怎么移动,都没有办法使得石子数量增多!那么做法就简单了,如果石子数量比没改动前的多,那么就不合法了. Code #include<iostream> using namespace std; int sum1=0,su

Codeforces 游记

早就对这个比赛平台有所耳闻(事实上,之前打过一场div2惨的一批……)今天去打了一场div3. 首先还是吐槽一下这个毛子时区的比赛时间,从十点三十五到零点三十五……这种时间要不是在家根本没法打嘛…… 先贴一下丢人的Preliminary results. 开局A题签到,用map当哈希表过了B2(@Rorschach_XR说过,STL离散化建议上map(笑)) 然后读一下C题,花里胡哨好像不是很可做,直接去看D题.给出一个字符串,要求支持两种操作:修改一个字符:给出一个区间,求区间内不同字符的个数

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store