[POJ2342]Anniversary party

OJ题号:POJ2342、洛谷1352

思路:树形动规(递归实现),每次返回一个pair,分别记录包括本人的最大值和不包括本人的最大值,每次如果遇到conviviality rating为负的就忽略。

 1 #include<cstdio>
 2 #include<vector>
 3 #include<algorithm>
 4 const int N=6001;
 5 std::vector<int> c[N];
 6 int r[N],p[N]={0};
 7 inline void add_edge(const int a,const int b) {
 8     c[a].push_back(b);
 9     p[b]=a;
10 }
11 std::pair<int,int> dfs(const int p) {
12     int w1=r[p],w2=0;
13     for(unsigned int i=0;i<c[p].size();i++) {
14         std::pair<int,int> t=dfs(c[p][i]);
15         if(t.second>0) w1+=t.second;
16         if(t.first>0) w2+=t.second?std::max(t.first,t.second):t.first;
17     }
18     return std::make_pair(w1,w2);
19 }
20 int main() {
21     int n;
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++) scanf("%d",&r[i]);
24     for(int i=1;i<n;i++) {
25         int l,k;
26         scanf("%d%d",&l,&k);
27         add_edge(k,l);
28     }
29     int root;
30     for(int i=1;i<=n;i++) {
31         if(!p[i]) {
32             root=i;
33             break;
34         }
35     }
36     std::pair<int,int> ans=dfs(root);
37     printf("%d\n",std::max(ans.first,ans.second));
38     return 0;
39 }
时间: 2024-12-30 17:59:57

[POJ2342]Anniversary party的相关文章

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

[poj2342]Anniversary party_树形dp

Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形dp 树形dp就是以节点或者及其子树为信息,进行动态规划.用dfs的原理,遍历,在回溯是更新父亲节点. 然后,关于这道题,我们就可以对于每一个节点进行标记,然后对于满足条件的节点进行遍历.设状态就是两个dp,分别表示选当前根节点和不选当前根节点.更新是瞎jb更新即可... .... 最后,附上丑陋的代码...

poj2342 Anniversary party 简单树形dp

http://poj.org/problem?id=2342 Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4322   Accepted: 2459 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Universi

Hdoj 1520&amp;Poj2342 Anniversary party 【树形DP】

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

poj2342 Anniversary party【树形dp】

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

DP Intro - Tree POJ2342 Anniversary party

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 U

[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

树形dp系列

hdu 1520,poj2342 Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12664    Accepted Submission(s): 5106 Problem Description There is going to be a party to celebrate the 80-th A

hdu1520 Anniversary party(poj2342,树形dp)

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