博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2342 Anniversary party 简单树形dp
阅读量:5128 次
发布时间:2019-06-13

本文共 3189 字,大约阅读时间需要 10 分钟。

 

Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3862   Accepted: 2171

 

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. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.

Input

Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form: 
L K 
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 
0 0 

Output

Output should contain the maximal sum of guests' ratings.

Sample Input

711111111 32 36 47 44 53 50 0

Sample Output

5

【题意】

公司有n个人,每个人有价值vi,有一天举办年会,每个人都可以参加,但有严格的等级制度,参加活动时,不能同时出现a和a的上司,问如何才能使总和最大。

【分析】

每个人只有去和不去两种状态,设DP[i][0]和DP[i][1]分别表示第i个人不参加和参加年会,获得的总的最大价值。

则状态转移方程为:

    DP[i][1] += DP[j][0],

    DP[i][0] += max{DP[j][0],DP[j][1]};其中j为i的孩子节点。

    这样,从根节点r进行dfs,最后结果为max{DP[r][0],DP[r][1]}。

(分析来自)

第2~n+1行为这n个人的价值

代码:

1 #include "stdio.h"   //简单的树形dp题 2 #include "string.h" 3 #include "queue" 4 using namespace std; 5  6 #define N 60005 7 #define INF 0x3fffffff 8  9 struct node10 {11     int x,y;12     int weight;13     int next;14 }edge[4*N];15 int idx,head[N];16 17 int root;18 int du[N];19 int value[N];20 21 int dp[N][2];22 int MAX(int a,int b) { return a>b?a:b; }23 24 void Init()25 {26     idx = 0;27     memset(head,-1,sizeof(head));28 }29 30 void Add(int x,int y,int weight)31 {32     edge[idx].x = x;33     edge[idx].y = y;34     edge[idx].weight = weight;35     edge[idx].next = head[x];36     head[x] = idx++;37 }38 39 void DFS(int i)  //40 {41     int k,j;42     dp[i][0] = 0;43     dp[i][1] = value[i];44     for(k=head[i]; k!=-1; k=edge[k].next)45     {46         j = edge[k].y;47         DFS(j);48         dp[i][0] += MAX(dp[j][0],dp[j][1]);49         dp[i][1] += dp[j][0];50     }51 }52 53 54 int main()55 {56     int n;57     int i;58     int x,y;59     while(scanf("%d",&n)!=EOF)60     {61         Init();62         memset(du,0,sizeof(du));  //记录节点的入度63         memset(dp,0,sizeof(dp));64         for(i=1; i<=n; ++i)65             scanf("%d",&value[i]);66         while(scanf("%d %d",&x,&y) && x+y>0)67         {68             Add(y,x,0);69             du[x]++;70         }71         for(i=1; i<=n; ++i)72         {73             if(du[i]==0)74                 root = i;75         }76         DFS(root);77         printf("%d\n",MAX(dp[root][0],dp[root][1]));78     }79     return 0;80 }

 

 

 

转载于:https://www.cnblogs.com/ruo-yu/p/4411977.html

你可能感兴趣的文章
Eclipse Python插件 PyDev
查看>>
selenium+python3模拟键盘实现粘贴、复制
查看>>
第一篇博客
查看>>
typeof与instanceof的区别
查看>>
网站搭建(一)
查看>>
SDWebImage源码解读之SDWebImageDownloaderOperation
查看>>
elastaticsearch
查看>>
postgreSQL 简单命令操作
查看>>
Spring JDBCTemplate
查看>>
Radon变换——MATLAB
查看>>
第五章笔记
查看>>
Iroha and a Grid AtCoder - 1974(思维水题)
查看>>
gzip
查看>>
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
[LeetCode] Palindrome Number
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
SQL更新某列包含XX的所有值
查看>>
网易味央第二座猪场落户江西 面积超过3300亩
查看>>