HDU 1520 Anniversary party (树形DP,入门)

题意:给一棵树,每个节点都有权值,要求选择部分节点出来,使得权值之和最大,但是每对(父亲,儿子)中最多只能挑一个。

思路:

  比较入门的题,每个节点可以选也可以不选。若当前节点选的话,孩子必须全部不选;若当前节点不选,则孩子可以选也可以不选。

 1 #include <bits/stdc++.h>
 2 #define pii pair<int,int>
 3 #define INF 0x3f3f3f3f
 4 #define LL long long
 5 using namespace std;
 6 const int N=7010;
 7 int w[N], dp[N][2], pre[N];
 8 vector<int> tree[N];
 9
10 void DFS(int t)
11 {
12     for(int i=0; i<tree[t].size(); i++)    DFS(tree[t][i]);
13     int sum[2]={0,0};
14     for(int i=0; i<tree[t].size(); i++)
15     {
16         sum[0]+=max(dp[tree[t][i]][0], dp[tree[t][i]][1]); //点t尝试不取,则孩子可以取也可以不取,取大者即可。
17         sum[1]+=max(dp[tree[t][i]][0], 0);    //点t尝试取,则孩子必须全部不取
18     }
19     dp[t][0]=sum[0];
20     dp[t][1]=sum[1]+w[t];
21 }
22
23
24 int main()
25 {
26     //freopen("input.txt", "r", stdin);
27     int n, a, b, root;
28     while(cin>>n)
29     {
30         memset(pre,0,sizeof(pre));
31         memset(dp,0,sizeof(dp));
32         for(int i=0; i<=n; i++) tree[i].clear();
33
34         for(int i=1; i<=n; i++) scanf("%d",&w[i]);
35         while(scanf("%d%d",&a,&b),a+b)
36         {
37             tree[b].push_back(a);
38             pre[a]=b;
39         }
40         for(int i=1; i<=n; i++) if(pre[i]==0)   root=i;
41         DFS(root);
42         printf("%d\n", max(dp[root][0],dp[root][1]) );
43     }
44     return 0;
45 }

AC代码

时间: 2024-10-14 00:30:12

HDU 1520 Anniversary party (树形DP,入门)的相关文章

POJ 2342 &amp;&amp;HDU 1520 Anniversary party 树形DP 水题

一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点)都不能被邀请 2.每一个员工都有一个兴奋值,在满足1的条件下,要使得邀请来的员工的兴奋值最高 输出最高的兴奋值. 简单的树形DP dp[i][1]:表示以i为根的子树,邀请节点i的最大兴奋值 dp[i][0]:表示以i为根的子树,不邀请节点i的最大兴奋值 先根据入度找出整棵树的根节点, 然后一次DF

HDU 1520 Anniversary party (树形DP)

树形DP的关键在于如何处理递归返回的信息.这题dp[i][0]表示不选i点时当前最高权值.dp[i][1]表示选i点时当前最高权值.状态转移方程:dp[u][[0]+=max(dp[v][0],dp[v][1]),dp[u][1]+=dp[v][0]; 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm&

hdu oj 1520 Anniversary party(树形dp入门)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6990    Accepted Submission(s): 3104 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

[poj2342]Anniversary party树形dp入门

题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max(dp[j][0],dp[j][1]); 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<iostream> 6

Anniversary party(树形DP入门)

Anniversary party HDU - 1520 There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rect

HDU - 1520 Anniversary party (有向入门树形DP)

题目链接:https://vjudge.net/problem/HDU-1520 There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted

POJ 2342 Anniversary party (树形dp 入门题)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4810   Accepted: 2724 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

HDU 1520-Anniversary party(树形dp入门)

题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1,i参加 能达到的最大欢乐值.i参加i的孩子不能参加,i不参加,其孩子参不惨加都行(取最大值). #include <map> #include <set> #include <list> #include <cmath> #include <queue&

hdu 1011 Starship Troopers(树形DP入门)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17498    Accepted Submission(s): 4644 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of t