G将军的敢死队——树状DP

当前节点的两种情况:

1.beChoosed = {son.beAbandoned乘积}  //当前节点选中的情况下,子节点都不能选

2.beAbandoned = {(son.beAbandoned + son.beChoosed)乘积} //当前节点不选的情况下,子节点所有情况都算上

ps:最终要减去一种所有节点都未被选中的情况  -1

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <list>
 4 #define MOD 10007
 5 #define MAXN 100002
 6 using namespace std;
 7 int n;
 8 struct node{
 9     int parent;
10     list<int>son;
11     int son_count;
12     int beChoosedCount,beAbandonedCount;
13 }num[MAXN];
14
15 void dfs(int id)
16 {
17     for(int i=0;i<num[id].son_count;++i)
18     {
19         int tmp = num[id].son.front();
20         dfs(tmp);
21         num[id].son.pop_front();
22         num[id].beChoosedCount *= num[tmp].beAbandonedCount%MOD;
23         num[id].beAbandonedCount *= (num[tmp].beChoosedCount+num[tmp].beAbandonedCount)%MOD;
24     }
25
26 }
27 int main()
28 {
29     cin>>n;
30     num[1].beChoosedCount=1;
31     num[1].beAbandonedCount=1;
32     for (int i = 2; i <= n; ++i)
33     {
34
35         cin>>num[i].parent;
36         num[num[i].parent].son.push_back(i);
37         num[num[i].parent].son_count++;
38         num[i].beAbandonedCount=1;
39         num[i].beChoosedCount=1;
40     }
41
42     dfs(1);
43     cout<<(num[1].beAbandonedCount+num[1].beChoosedCount-1)%MOD;
44     return 0;
45 }
时间: 2024-10-29 01:01:54

G将军的敢死队——树状DP的相关文章

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

POJ 1463 树状dp

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6629   Accepted: 3058 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

洛谷P1122 最大子树和 (树状dp)

题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明就向老师提出了这个问题: 一株奇怪的花卉,上面共连有N 朵花,共有N-1条枝干将花儿连在一起,并且未修剪时每朵花都不是孤立的.每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美丽指数”为负 数的,说明这朵花看着都让人恶心.所谓“修剪”,意为:去掉其中的一条枝条,这样一株花就成了两株,扔掉

POJ 2342 树状dp

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4606   Accepted: 2615 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

poj1947--Rebuilding Roads(树状dp)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9496   Accepted: 4316 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

洛谷P2015 二叉苹果树(树状dp)

题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树 2 5 \ / 3 4 \ / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 输入输出格式 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=100). N表示树

树状DP入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给定一棵关系树,每个节点有个权值,子节点和父节点不能同时选,问最后能选的最大价值是多少? 解题思路:树形DP入门题.由于子节点与父节点不能同时选,有人可能会用贪心思想,二者选其一肯定最优.其实不然,有可能父节点和子节点都不选,而要选子孙节点.不过只要再往深点想下,就可以得出动态规划的解法.每个节点要么选要么不选,和大多数选不选动归一样,来个dp[i][2],0表示不选,1表示不选,那

树状DP (poj 2342)

题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[i][0] += max(F[k][1], F[k][0]);(k为i的儿子) 第二种情况:第i个员工参与,F[i][1] += F[k][0]; F[i][j]表示第i个员工是否参与: 边界:F[i][0] = 0:F[i][1] = 其快乐指数: #include <iostream> #in

poj2486--Apple Tree(树状dp)

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7789   Accepted: 2606 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amoun