Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6990 Accepted Submission(s): 3104Problem DescriptionThere 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.InputEmployees 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 0OutputOutput should contain the maximal sum of guests' ratings.Sample Input711111111 32 36 47 44 53 50 0Sample Output5Source
题意:n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,
问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int N = ;
int dp[N][],root[N];
vector<int>G[N];
void DFS(int cur)
{
for(int i = ; i < (int)G[cur].size(); i++)
{
int to = G[cur][i];
DFS(to);
dp[cur][] += dp[to][];//该级去,下一级不去
dp[cur][] += max(dp[to][],dp[to][]);//该级不去,下级去不去选最优
}
} int main(void)
{
int n;
while(scanf("%d",&n) != EOF)
{
memset(dp,,sizeof(dp));
for(int i = ; i <= n; i++)
{
scanf("%d",&dp[i][]);//代表第几个人去能增加的活跃度
root[i] = i;//并查集的初始化
G[i].clear();
}
int a,b;
while(scanf("%d %d",&a,&b),a != ||b != )
{
G[b].push_back(a);//vector建图
root[a] = b;
}
int beg;
for(int i = ; i <= n; i++)
{
if(root[i] == i)//查找根节点
{
beg = i;
break;
}
}
DFS(beg);
printf("%d\n",max(dp[beg][],dp[beg][]));
}
return ;
}