HDU1520 Anniversary party 树形DP基础
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 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
水题一道,晚安。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<memory>
using namespace std;
const int maxn=; int vis[maxn],V[maxn],ans,cnt,dp[maxn][];
int Laxt[maxn],Next[maxn],To[maxn],ru[maxn]; void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
} int dfs(int u)
{
vis[u]=;
dp[u][]=V[u];
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!vis[v]){
dfs(v);
dp[u][]+=dp[v][];
dp[u][]+=max(dp[v][],dp[v][]);
}
}
} int main()
{
int i,j,k,n,u,v;
while(~scanf("%d",&n)){
cnt=;ans=;
memset(Laxt,,sizeof(Laxt));
memset(dp,,sizeof(dp));
memset(ru,,sizeof(ru));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++) scanf("%d",&V[i]);
for(;;){
scanf("%d%d",&u,&v);
if(u==&&v==) break;
add(v,u);
ru[u]++;
}
for(i=;i<=n;i++) {
if(ru[i]==) {
dfs(i);
ans+=max(dp[i][],dp[i][]);//防止多棵树
}
}
printf("%d\n",ans);
}
return ;
}
HDU1520 Anniversary party 树形DP基础的更多相关文章
- HDU1520 Anniversary party —— 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- hdu1520 Anniversary party (树形dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- hdu1520 第一道树形DP,激动哇咔咔!
A - 树形dp Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- [poj2342]Anniversary party_树形dp
Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 树形dp基础
今天来给大家讲一下数形dp基础 树形dp常与树上问题(lca.直径.重心)结合起来 而这里只讲最最基础的树上dp 1.选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程 ...
随机推荐
- git-svn — 让git和svn协同工作
git-svn — 让git和svn协同工作 svn作为一个优秀源码版本的管理工具,可以适合绝大多数项目.但是因为它的采用中心化管理,不可避免的存在本地代码的备份和版本管理问题.也就是说对于尚未或暂 ...
- hdu 4602 Partition 矩阵快速幂
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- vue-cli background iamge
vue-cli 可以将图片直接放在项目生成的 static 文件夹里,然后在components里面直接采用绝对路径去取就可以了. 在根目录里面都会有一个static目录,这个是用来存放静态文件的,把 ...
- iOS学习-字符串的删除替换
字符串的常用处理,删除,替换.记录一下,方便查找. -------------------------------------------------------------------------- ...
- LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)
题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...
- django from验证组件
from django.shortcuts import render,redirect from django.forms import Form,fields class loginForm(Fo ...
- 开发H5游戏引擎的选择:Egret或Laya?
开发H5游戏引擎的选择:Egret或Laya? 一.总结 一句话总结:选laya吧 二.开发H5游戏引擎的选择:Egret或Laya? 一.H5游戏开发的引擎介绍 开发H5游戏的引擎有很多,比如egr ...
- Apache 2 移植到Arm开发板
第一步,安装pcre: tar -xvzf pcre-8.31.tar.gz cd pcre-8.31 ./configure --prefix=$ARMROOTFS/usr/pcre 的错误,如下图 ...
- jQuery 获取、设置表单元素的值
获取表单元素值: 文本框,文本区域: $("#txt").attr("value"): 多选框 checkbox:$("#checkbox_id&qu ...
- Codeforces Round #424
基本全是水题 第一题水,不过有hack点,先增后不变再减 #include<map> #include<set> #include<cmath> #include& ...