Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6990    Accepted Submission(s): 3104

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
 
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

题意: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 ;
}


最新文章

  1. mongodb管理篇
  2. switch 与 python字典
  3. 用SQLMAP工具进行SQL注入
  4. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)
  5. ee
  6. 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换
  7. NBUT 1120 Reimu&#39;s Teleport (线段树)
  8. php的header()大全
  9. OC协议
  10. 编写可维护的javascript代码---开篇(介绍自动报错的插件)
  11. 谈如何使用c中的qsort快速排序库函数 按主次关键字正确排序
  12. Docker 安装命令
  13. 【剑指offer】树的子结构
  14. Mac下配置node.js环境(Mac 10.12)
  15. HTML——超文本标记语言(表单及12个表单元素)
  16. windows系统中给qt工程添加第三方库
  17. 贪吃蛇游戏——C语言双向链表实现
  18. 4QC(四象限变流器)
  19. flask框架----数据库连接池
  20. SQL-26 (二次分组)汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count

热门文章

  1. vc 获取窗口标题GetWindowText
  2. leetcode-第5周双周赛-1135-最低成本联通所有城市
  3. 解决CentOS“Zabbix discoverer processes 75% busy”的问题
  4. java.sql.SQLException
  5. 国内有哪些质量高的JAVA社区?
  6. Template-Thymeleaf:Thymeleaf
  7. “Error: Encountered an improper argument”的解决方法
  8. 报错initscripts conflicts with redhat-release-server-7.0-1.el7.x86_64
  9. 《DSP using MATLAB》Problem 8.18
  10. java浮点运算的陷阱