HDU 1520 Anniversary party

http://acm.hdu.edu.cn/showproblem.php?pid=1520

题意:

将有一个党庆祝乌拉尔国立大学80周年。大学有一个员工的层次结构。这意味着监督关系形成了一个树根源于校长VE Tretyakov。为了使党对每个人都有趣,主任不想让一个雇员和他或她的直接主管在场。人事局已评估每个员工的欢乐性,所以每个人都有一定数量(评级)附加到他或她。你的任务是制作一个客人的列表,其中有最大可能的客人的欢乐度评分的总和。

思路:

这道题目就是UVa 1220的减弱版,比较基本的树形DP。

d[u][1]代表以u为根的子树中,选u能得到的最大欢乐度。d[u][0]代表不选u能得到的最大欢乐度。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7
 8 int n;
 9 int a[6005];
10 int d[6005][2];
11
12 vector<int> sons[6005];
13
14 void dfs(int u)
15 {
16     if (sons[u].size() == 0)
17     {
18         d[u][1] = a[u];
19         d[u][0] = 0;
20         return;
21     }
22
23     if (d[u][1])   return;
24
25     int k = sons[u].size();
26     for (int i = 0; i < k; i++)
27     {
28         int son = sons[u][i];
29         dfs(son);
30         d[u][1]+= d[son][0];
31
32         d[u][0]+= max(d[son][1], d[son][0]);
33     }
34     d[u][1] += a[u];
35 }
36
37 int main()
38 {
39     //freopen("D:\\txt.txt", "r", stdin);
40     int x, y;
41     while (cin >> n)
42     {
43         memset(d, 0, sizeof(d));
44         for (int i = 1; i <= n; i++)
45         {
46             cin >> a[i];
47             sons[i].clear();
48         }
49         while (cin >> x>> y)
50         {
51             if (x == 0 && y == 0)  break;
52             sons[y].push_back(x);
53         }
54
55         int Max = 0;
56         for (int i = 1; i <= n; i++)
57         {
58             dfs(i);
59             Max = max(Max, max(d[i][1], d[i][0]));
60         }
61         cout << Max << endl;
62     }
63     return 0;
64 }
时间: 2024-10-14 22:44:46

HDU 1520 Anniversary party的相关文章

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>

hdu 1520 Anniversary party(第一道树形dp)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16376    Accepted Submission(s): 6241 Problem Description There is going to

HDU 1520.Anniversary party 基础的树形dp

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

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 || codevs 1380 树形dp

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical

HDU 1520 Anniversary party(树形DP)

Anniversary party Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 81   Accepted Submission(s) : 31 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description There is going to b

HDU 1520 Anniversary party(DFS或树形DP)

Problem 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 of employees. It means that the supervisor relation forms a tree rooted at the rector V. E.

hdu 1520 Anniversary party 生日party 树形dp第一题

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

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