【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">【HDU 5647】DZY Loves Connecting(树DP)
DZY Loves Connecting
Total Submission(s): 332 Accepted Submission(s): 112
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.
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)
109+7.
2
1
5
1
2
2
3
1
42HintIn 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++.
一个挺直接的树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)的更多相关文章
- HDU 5647 DZY Loves Connecting 树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5647 题解: 令dp[u][0]表示u所在的子树中所有的包含i的集合数,设u的儿子为vi,则易知dp ...
- HDU5647 DZY Loves Connecting 树形DP
(先奉上jcvb大神的官方题解)BC 76 div 1 1002 对于每个结点i,统计出f[i]表示包含i的连通集有多少个,那么容易看出答案就是所有f[i]的和. 要计算f[i]是经典的树形DP问题. ...
- HDU 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- 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 ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- 数据结构(线段树):HDU 5649 DZY Loves Sorting
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
随机推荐
- python测试开发django-20.添加创建时间DateTimeField
前言 我们在admin后台发布一篇文章的时候,一般会有创建时间和最后更新时间这2个字段,创建时间就是第一次编辑文章的时候自动添加的,最后更新时间就是每次修改文章的内容后自动更新 在models.py建 ...
- 开源项目MultiChoiceAdapter详解(五)——可扩展的MultiChoiceBaseAdapter
上次写到了开源项目MultiChoiceAdapter详解(四)——MultiChoiceBaseAdapter的使用,其实我们仍旧可以不使用ActionMode的,所以这里就写一个自己扩展的方法. ...
- c# action<> func<> 这2个委托怎么用和理解
其实很好理解的呢!~首先你需要明白,他们是委托的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 1.1定义一个委托: 比如,我们原来写委托: public de ...
- [Android Pro] 跨平台反编译工具 jadx (ubuntu亲测 可用)
github : https://github.com/skylot/jadx 输入: .dex, .apk, .jar or .class 输出: 资源文件 和 class文件 不支持重新打包, ...
- 利用js实现 禁用浏览器后退 浏览器返回
现在很多的内部系统,一些界面,都是用户手动点击退出按钮的.但是为了避免,用户误操作 点击浏览器后退,或者用鼠标手势后退什么的.容易出现误操作. 所以在有些页面上,适当的禁用浏览器的后退, ...
- 里氏替换原则(Liskov Substitution Principle,LSP)
肯定有不少人跟我刚看到这项原则的时候一样,对这个原则的名字充满疑惑.其实原因就是这项原则最早是在1988年,由麻省理工学院的一位姓里的女士(Barbara Liskov)提出来的. 定义1:如果对每一 ...
- iOS:自定义导航栏,随着tableView滚动显示和隐藏
自定义导航栏,随着tableView滚动显示和隐藏 一.介绍 自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果.虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因 ...
- C# DevExpress GridControl导出表格【转】
DevExpress的GridControl提供方便的数据导出功能,可以方便的导出Exce,PDF,Html页面,world形式,无需写额外的代码,方便.快捷. /// <summary> ...
- Java面试问题总结
前几天Java面试中遇到的问题,这仅仅是当中的一部分问题.面试中有非常多问题是关于数据结构和算法的.在这里做下总结,希望有能力的人能够试着做一下,并在评论区留下您的答案.让大家相互学习.谢谢 程序设计 ...
- Laravel5.5 Jwt 1.0 beta 配置
https://github.com/tymondesigns/jwt-auth/issues/860 1 下载开发者版本 image.png 修改composer.json,添加 "t ...