hdu 1520
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6401 Accepted Submission(s): 2862
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.
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
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define sfl(n) scanf("%lld", &n)
#define pfi(n) printf("%d\n", n)
#define pfl(n) printf("%lld\n", n)
#define MAXN 6005
int w[MAXN];
bool f[MAXN] = {false};
vector<int> v[MAXN];
int d[MAXN][];
bool vis[MAXN] = {false};
void dp(int root)
{
if(vis[root]) return ;
vis[root] = true;
int siz = v[root].size();
repu(i, , siz) dp(v[root][i]); repu(i, , siz)
{
d[root][] += max(d[v[root][i]][], d[v[root][i]][]);
d[root][] += d[v[root][i]][];
}
return ;
}
int main()
{
int n;
while(~sfi(n))
{
_cle(vis, false);
_cle(f, false);
repu(i, , n + )
{
sfi(w[i]);
v[i].clear();
d[i][] = ;
d[i][] = w[i];
}
int x, y;
while(sfi(x), sfi(y), x + y)
{
v[y].push_back(x);
f[x] = true;
}
int root;
repu(i, , n + ) if(!f[i])
{
root = i;
break;
}
dp(root);
pfi(max(d[root][], d[root][]));
//repu(i, 1, n + 1) printf("%d %d\n", d[i][0], d[i][1]);
} return ;
}
hdu 1520的更多相关文章
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- HDU 1520:Anniversary party(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description There i ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1520(简单树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- hdu 1520 树形DP基础
http://acm.hdu.edu.cn/showproblem.php?pid=1520 父节点和子节点不能同时选. http://blog.csdn.net/woshi250hua/articl ...
随机推荐
- [SAP ABAP开发技术总结]字段符号FIELD-SYMBOLS
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)
python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...
- 虚拟机安装Centos64位Basic Service后 ifconfig查看无ip
vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ONBOOT="no" 改为 ONBOOT="yes" 保存后: ...
- AFNetworking请求中含有中文时程序崩溃
出现error: Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error ...
- UVA 11468【AC自动机+DP】
dp[i][j]表示走了i步走到j结点的概率.初始值dp[0][0] = 1.当走到的结点不是单词尾结点时,才能走过去. !end[i]&&last[i] == root时,该结点才可 ...
- mysql 求时间段平均值
考虑下面的需求,在一段时间内,间隔一段时间,取一个平均值,把所有的平均值取出来,怎么办?思路:在存储过程中,拼接sql语句.根据起始时间和结束时间,while循环每次加一段时间.DROP PROCED ...
- ES6中的const命令
1.const声明一个只读常量,一旦声明,常量的值就不能改变 1 const PI=3.1415; 2 console.log(PI);//3.1415 3 4 PI=3;//Uncaught T ...
- xutils使用过程
1,导入xutils的jar包 2,添加xutils需要使用的权限 <uses-permission android:name="android.permission.INTERNET ...
- flume Permission denied: user=flume, access=WRITE, inode
My flume app is attempting to write to HDFS on a path thats not been created/granted for it. The pat ...
- 项目开发中遇到的extjs常见问题
事件触发机制 l 给某一个控件添加事件. obj.addEvents( {search : true }); l 给某一个事件添加处理函数 n 给一个对象或变量添加监听及对应得处理,可以在创建时,通过 ...