pid=5647">【HDU 5647】DZY Loves Connecting(树DP)

DZY Loves Connecting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 332    Accepted Submission(s): 112

Problem Description
DZY has an unrooted tree consisting of
n
nodes labeled from 1
to n.



DZY likes connected sets on the tree. A connected set
S
is a set of nodes, such that every two nodes u,v
in S
can be connected by a path on the tree, and the path should only contain nodes from
S.
Obviously, a set consisting of a single node is also considered a connected set.



The size of a connected set is defined by the number of nodes which it contains. DZY wants to know the sum of the sizes of all the connected sets. Can you help him count it?



The answer may be large. Please output modulo 109+7.
 
Input
First line contains t
denoting the number of testcases.

t
testcases follow. In each testcase, first line contains
n.
In lines 2∼n,
ith
line contains pi,
meaning there is an edge between node i
and node pi.
(1≤pi≤i−1,2≤i≤n)



(n≥1。
sum of n
in all testcases does not exceed 200000)
 
Output
Output one line for each testcase, modulo
109+7.
 
Sample Input
2
1
5
1
2
2
3
 
Sample Output
1
42
Hint
In the second sample, the 4 edges are (1,2),(2,3),(2,4),(3,5). All the connected sets are {1},{2},{3},{4},{5},{1,2},{2,3},{2,4},{3,5},{1,2,3},{1,2,4},{2,3,4},{2,3,5},{1,2,3,4},{1,2,3,5},{2,3,4,5},{1,2,3,4,5}. If you need a larger stack size,
please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 
Source
 

一个挺直接的树DP。之前TC的一个原题,。比赛时死活没啃出来。。

。太嫩了……

题目大意:给出一个树,详细建法就不细说了。每一个点贡献为1,问树上全部不同集合的合计贡献。

首先对于每一个点,我们能够求出它所在的集合数量。

开个数组cnt。表示第i个点在其子树中所在的集合数。这样cnt[i]就等于i全部孩子的cnt+1的乘积。事实上这里用到了组合,从全部孩子中能够随意选择一定的集合(+1表示空集)进行组合。

求出集合数是为了求贡献,遍历到i的某个孩子的时候,新添加的贡献事实上就是之前的全部贡献乘上该孩子的集合数+1(选择某些集合组合),然后对于之前遍历的子数。该孩子也能够提供贡献,也就是该孩子的贡献乘上当前出现的集合数。

口述表达的不好,也不会做那种图。

。还是看代码吧,就俩公式。。

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; struct Edge
{
int v,next;
}; Edge eg[233333];
int head[233333];
//0存当前节点往下包括当前节点的区间数 1存当前节点的子树中全部包括当前节点的贡献
LL dp[233333][2];
LL ans; void dfs(int u)
{
dp[u][1] = 1;
dp[u][0] = 1;
for(int i = head[u]; i != -1; i = eg[i].next)
{
dfs(eg[i].v);
//当前点子树中包括当前点的区间的贡献
dp[u][1] = (dp[u][1]*(dp[eg[i].v][0]+1)+dp[eg[i].v][1]*dp[u][0])%mod;
dp[u][0] = (dp[u][0]*(dp[eg[i].v][0]+1))%mod;
}
ans = (ans+dp[u][1])%mod;
} int main()
{
//fread();
//fwrite(); int x,t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(head,-1,sizeof(head));
for(int i = 2; i <= n; ++i)
{
scanf("%d",&x);
eg[i].v = i;
eg[i].next = head[x];
head[x] = i;
}
ans = 0;
dfs(1);
printf("%lld\n",ans);
} return 0;
}

【HDU 5647】DZY Loves Connecting(树DP)的更多相关文章

  1. HDU 5647 DZY Loves Connecting 树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5647 题解: 令dp[u][0]表示u所在的子树中所有的包含i的集合数,设u的儿子为vi,则易知dp ...

  2. HDU5647 DZY Loves Connecting 树形DP

    (先奉上jcvb大神的官方题解)BC 76 div 1 1002 对于每个结点i,统计出f[i]表示包含i的连通集有多少个,那么容易看出答案就是所有f[i]的和. 要计算f[i]是经典的树形DP问题. ...

  3. HDU 5646 DZY Loves Partition

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  4. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  5. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  6. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  7. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  8. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

随机推荐

  1. node.js模块的坑

    在写一个工具的时候,需要将xml转为json方便处理,以前电脑上装的node.js的版本为0.8,结果我再安装node-xml2json时提示版本过低,然后我又重装安装了最新版本. 然后再次尝试安装, ...

  2. 详细解释如何通过Android自带的方式来实现图片的裁剪——原理分析+解决方案

    我们很多时候需要进行图片的裁剪,其实这个功能在android系统中已经有一套解决方案了,虽然界面和效果并不是很优秀但功能毫无疑问是完美实现了.至于,不用自带的方案怎么做自定义,这个就是后话了.本篇主要 ...

  3. 【转】memcached分布式部署

    FROM : http://www.tuicool.com/articles/777nE3j memcache和memcached两者使用起来几乎一模一样. $mem = new Memcache; ...

  4. System.Reflection.TargetException:“非静态方法需要一个目标。”

    报错:TargetException, 非静态方法需要一个目标,非静态方法 如果实例为null,调用实例方法会报如上错. 解决办法: 检查实例是否为null,考虑什么情况下实例为null,然后排除实例 ...

  5. 生成学习算法(Generative Learning algorithms)

    一.引言 前面我们谈论到的算法都是在给定\(x\)的情况下直接对\(p(y|x;\theta)\)进行建模.例如,逻辑回归利用\(h_\theta(x)=g(\theta^T x)\)对\(p(y|x ...

  6. codeforce 192 div2解题报告

    今天大家一起做的div2,怎么说呢,前三题有点坑,好多特判.... A. Cakeminator 题目的意思是说,让你吃掉cake,并且是一行或者一列下去,但是必须没有草莓的存在.这道题目,就是判断一 ...

  7. SharePoint自定义程序页面部署 不用重启IIS

    SharePoint的部署方式默认是部署WSP包,尤其是有多个前端的时候WSP包的部署显得非常方便和快捷,但是WSP的部署需要重启整个IIS服务会造成SharePoint站点一段时间不能访问.结合自己 ...

  8. jQuery EasyUI Datagrid性能优化专题(转)

    jQuery  EasyUI的Datagrid组件功能算是很强大了,不过性能确实不怎么乐观,而对于性能问题,网络上几乎也找不到相关的优化资料,所谓的牛人们可能 都望而却步了.本博客以后会带着分析Dat ...

  9. Angular2 -- 生命周期钩子

    组件生命周期钩子 指令和组件的实例有一个生命周期:新建.更新和销毁. 每个接口都有唯一的一个钩子方法,它们的名字是由接口名加上 ng前缀构成的.比如,OnInit接口的钩子方法叫做ngOnInit. ...

  10. jquery ajax 的 $.get()用法详解

    js文件 $(document).ready(function(){ $("form").submit(function(event) {event.preventDefault( ...