树形DP URAL 1039 Anniversary Party

题目传送门

 1 /*
 2     题意:上司在,员工不在,反之不一定。每一个人有一个权值,问权值和最大多少。
 3     树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程:
 4             dp[rt][0] += max (dp[son][1], dp[son][0]);    //上司不去
 5             dp[rt][1] += dp[son][0];                    //上司去,员工都不去
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <vector>
11 #include <cmath>
12 using namespace std;
13
14 const int MAXN = 6e3 + 10;
15 const int INF = 0x3f3f3f3f;
16 bool vis[MAXN];
17 vector<int> edge[MAXN];
18 int dp[MAXN][2];
19 int n;
20
21 void DFS(int rt)
22 {
23     vis[rt] = true;
24     dp[rt][0] = 0;
25     for (int i=0; i<edge[rt].size (); ++i)
26     {
27         int son = edge[rt][i];
28         if (!vis[son])
29         {
30             DFS (son);
31             dp[rt][0] += max (dp[son][1], dp[son][0]);
32             dp[rt][1] += dp[son][0];
33         }
34     }
35 }
36
37 int main(void)        //URAL 1039 Anniversary Party
38 {
39 //    freopen ("URAL_1039.in", "r", stdin);
40
41     while (scanf ("%d", &n) == 1)
42     {
43         memset (vis, false, sizeof (vis));
44         memset (dp, 0, sizeof (dp));
45         for (int i=1; i<=n; ++i)    scanf ("%d", &dp[i][1]);
46
47         int l, k;
48         while (scanf ("%d%d", &l, &k) == 2)
49         {
50             if (l == 0 && k == 0)    break;
51             vis[l] = true;
52             edge[l].push_back (k);
53             edge[k].push_back (l);
54         }
55
56         int root = 0;
57         for (int i=1; i<=n; ++i)
58         {
59             if (!vis[i])    {root = i;    break;}
60         }
61
62         memset (vis, false, sizeof (vis));    DFS (root);
63         printf ("%d\n", max (dp[root][0], dp[root][1]));
64     }
65
66     return 0;
67 }
时间: 2024-10-08 01:11:23

树形DP URAL 1039 Anniversary Party的相关文章

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

ural 1039 Anniversary Party

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

(树形DP) ural 1018

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

HDU-1520 Anniversary party (树形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(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.

poj2342 Anniversary party (树形dp)

poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   Accepted: 5250 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarc

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

ural 1039 树dp

http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical st

poj 2342 Anniversary party,树形DP easy

poj 2342 Anniversary party 没有上司的晚会 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数最大.但是,没有职员愿和直接上司一起与会. 程序名:party 输入格式: 第一行一个整数N.(1<=N<=6000) 接下来N行,第i+1行表示i号职员的快乐指数Ri.(-128<=Ri<=127) 接下来N-1行,每行输入