poj2342 Anniversary party

Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8028   Accepted: 4594

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

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample Output

5

Source

解题思路:
第一个树形dp的题、、、
思路还是比较好理解的。
每个人有来与不来两种状态,当来的时候,他的直接下属就不能来。----①
当他不来的时候,他的直接下属可以来,可以不来。----②
用dp[i][1]表示编号为i的人的时候,以它为根的子树所以产生的活跃值:由①可知dp[i][1]=dp[i][1]+dp[儿子节点][0];
用dp[i][0]表示编号为i的人不来的时候,以它为根的子树所产生的活跃值:由②可知dp[i][0]=dp[i][1]+max(dp[儿子节点][0],dp[儿子节点][1]);
 
 #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define FOR(i,x,n) for(long i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 60
#define MAX_M 1005 using namespace std; struct node{
int number;
int rating;
int sonNum;
int son[];
int father;
};
node tree[];
//int visable[6005];
int dp[][];//0表示不去,1表示去 void dfs(int root){
FOR(i,,tree[root].sonNum){
dfs(tree[root].son[i]);
dp[root][]+=max(dp[tree[root].son[i]][],dp[tree[root].son[i]][]);
dp[root][]+=dp[tree[root].son[i]][];
}
} int main()
{
//freopen("input1.txt", "r", stdin);
//freopen("data.out", "w", stdout);
int n;
//memset(visable,0,sizeof(visable));
memset(dp,,sizeof(dp));
scanf("%d",&n);
FOR(i,,n+){
tree[i].father=i;
tree[i].sonNum=;
}
FOR(i,,n+){
scanf("%d",&dp[i][]);
}
int t1,t2;
FOR(i,,n){
scanf("%d %d",&t1,&t2);
if(!(t1+t2)){
break;
}
tree[t1].father=t2;
tree[t2].son[tree[t2].sonNum++]=t1;
}
int t=;
while(tree[t].father!=t){
t=tree[t].father;
}
dfs(t);
printf("%d",max(dp[t][],dp[t][]));
//fclose(stdin);
//fclose(stdout);
return ;
}
上一篇:Linux C高级编程——网络编程之以太网(2)


下一篇:Python for循环语句