http://codeforces.com/contest/461

A.水题



B.太挫了,竟然被hack了一发。。。。



C.贪心。。竟然没看出来时哈夫曼编码问题



D.题目大意:给一棵树,每一个点为白色或黑色,切断一些边,使得每一个连通块有且仅有一个黑点,问划分方案数。



树形DP,

设状态:

dp[v][0]表示以v为根的子树中没有黑点,dp[v][1]有一个黑点。

状态方程:

dp[v][1] = dp[v][1]*dp[son][0] + dp[v][0]*dp[son][1] + dp[v][1]*dp[son][1]

dp[v][0] = dp[v][0]*dp[son][0] + dp[v][0]*dp[son][1]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int mod=1000000007;
  5. const int maxn = 100000 + 100;
  6. vector<int> g[maxn];
  7. int clr[maxn];
  8. LL dp[maxn][2];
  9. int n;
  10.  
  11. void dfs(int u, int p)
  12. {
  13. dp[u][clr[u]] = 1;
  14. for(int i=0; i<g[u].size(); ++i)
  15. {
  16. int &v = g[u][i];
  17. if(v==p) continue;
  18. dfs(v, u);
  19. dp[u][1] = (dp[u][1]*dp[v][0]%mod + dp[u][0]*dp[v][1]%mod + dp[u][1]*dp[v][1]%mod) % mod;
  20. dp[u][0] = (dp[u][0]*dp[v][1]%mod + dp[u][0]*dp[v][0]%mod) % mod;
  21. }
  22. }
  23. int main()
  24. {
  25. scanf("%d", &n);
  26. int x;
  27. for(int i=1; i<n; ++i)
  28. {
  29. scanf("%d",&x);
  30. g[i].push_back(x);
  31. g[x].push_back(i);
  32. }
  33. for(int i=0; i<n; ++i)
  34. {
  35. scanf("%d", &clr[i]);
  36. }
  37.  
  38. dfs(0, -1);
  39. printf("%I64d\n", dp[0][1]);
  40. return 0;
  41. }

E.





Div1 D Appleman and Complicated Task





Div1 E Appleman and a Game

Codeforces Round #263的更多相关文章

  1. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  2. Codeforces Round #263 (Div. 1)

    B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...

  3. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  4. codeforces Round #263(div2) D. Appleman and Tree 树形dp

    题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...

  5. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

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

  7. Codeforces Round #263 (Div. 2) A B C

    题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...

  8. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  9. Codeforces Round #263 (Div. 2) proC

    题目: C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. System.DateTime的一些格式

    //2008年4月24日     System.DateTime.Now.ToString("D"); //2008-4-24     System.DateTime.Now.To ...

  2. Oracle EBS-SQL (SYS-4):sys_职责查询.sql

    select t.RESPONSIBILITY_NAME from apps.FND_RESPONSIBILITY_VL t where t.RESPONSIBILITY_NAME like '%MR ...

  3. 转: Apache开启gzip

    Apache开启gzip gzip是什么 HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度. 这一般是指WWW服务器 ...

  4. 运用JavaScript构建你的第一个Metro式应用程序(onWindows 8)(二)

    原文 http://blog.csdn.net/zhangxin09/article/details/6793330 先前的学习中,我们已经了解了 Metro式的 JavaScript 应用程序大致如 ...

  5. poj2105---用指针对数组分块操作

    #include <stdio.h> #include <stdlib.h> ; int pow1(int a,int b) { ,i; ) ; ;i<b;i++) { ...

  6. 【转】python import的用法

    [转自http://blog.sina.com.cn/s/blog_4b5039210100ennq.html] 在python用import或者from...import来导入相应的模块.模块其实就 ...

  7. Count the Colors(线段树,找颜色段条数)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  8. 什么时候css会见less

    在一定程度上,css不能被视为一个节目.虽然和其他语言,它有自己的规范.编码,但它的笨拙实在让我失望. 不喜欢css是由于不管怎么优化代码.项目大到一定程序后.都会看上去一团乱.并且有时候一个bug的 ...

  9. AlarmManager类的应用(实现闹钟功能)

    1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...

  10. C# 第三方控件 下面的Item不显示了

    当高版本的第三方版本 替换成低版本的第三方后,item,不显示了之后,请试着再次在这基础上添加一个Item,观察这个Item和原来已经在的却不显示的Item的区别在哪里.然后去源程序正常文件哪里 将这 ...