Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5338 Accepted Submission(s): 2459
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. 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 T 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
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
题意:某校长开了个爬梯,然后他们学校有一个等级森严的员工制度。然后他知道每个员工的快乐值,但是为了和谐,不会请员工和他上司同时出现在聚会上。问能请到所有员工最大的快乐值是多少。
题解:由于题目没有说清楚,我也不知道这个员工制度是不是每个人都必须在这个树里面,我就假设都在。然后任取一个点开始树形DP,0代表不请这个员工,1表示请这个员工。然后往后推0可以请0,或者1,而1只能请0了。脑洞开一下就想清楚了,然后取这个的最大值即可。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
int w;
struct node{
int a,b,next;
}edge[14000];
int head[14000];
int dp[7000][3];
int a[7000];
void add(int x,int y){
w++;
edge[w].a=x;
edge[w].b=y;
edge[w].next=head[x];
head[x]=w;
}
void DFS(int x,int y){
dp[x][0]=0;
dp[x][1]=a[x];
int i;
for (i=head[x];i!=-1;i=edge[i].next){
int to=edge[i].b;
if (to==y) continue;
DFS(to,x);
dp[x][0]+=max(dp[to][0],dp[to][1]);
dp[x][1]+=dp[to][0];
}
}
int main(){
int n,x,y,i;
while (~scanf("%d",&n)){
w=0;
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
memset(edge,0,sizeof(edge));
for (i=1;i<=n;i++) scanf("%d",&a[i]);
while (~scanf("%d%d",&x,&y) && (x&&y)){
add(x,y);
add(y,x);
}
DFS(1,-1);
printf("%d\n",max(dp[1][0],dp[1][1]));
}
return 0;
}