A - Anniversary party

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
此题同poj为一题,但是同样的代码pojAC,hdu却T了,题意就是聚会保证活跃度最大,但是限制具有上下等级关系的不能在一起,,,
开始用poj的代码一直T,后来改为邻接表93ms~~~~~~
5721163 qwerqqq A
Accepted
1760 93 1726
24 hr ago
5721115 qwerqqq A
Time Limit Exceeded
    1419
24 hr ago
5644872 qwerqqq A
Wrong Answer
    1332
11 days ago
5644870 qwerqqq A
Wrong Answer
    1334
11 days ago
5644868 qwerqqq A
Time Limit Exceeded
    1330
11 days ago
5644864 qwerqqq A
Time Limit Exceeded
    1328
11 days ago
5644862 qwerqqq A
Time Limit Exceeded
    1328
11 days ago
5644816 qwerqqq A
Time Limit Exceeded
    1417
11 days ago
5644809 qwerqqq A
Time Limit Exceeded
    1417
11 days ago
5644804 qwerqqq A
Time Limit Exceeded
    1354
11 days ago
 
 
 
 
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std; #define maxn 6005
struct node{
int to,net;
}que[maxn<<];
int head[maxn];
int n;
int dp[maxn][],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
bool visited[maxn];
int tot=; void addedge(int u,int v){
que[tot].to=v;
que[tot].net=head[u];
head[u]=tot++; que[tot].to=u;
que[tot].net=head[v];
head[v]=tot++; } void tree_dp(int node)
{
int i;
visited[node] = ;
for(i=head[node]; i!=-; i=que[i].net)
{
int v=que[i].to;
if(!visited[v]&&father[v] == node)//i为下属
{
tree_dp(v);//递归调用孩子结点,从叶子结点开始dp
//关键
dp[node][] += dp[v][];//上司来,下属不来
dp[node][] +=max(dp[v][],dp[v][]);//上司不来,下属来、不来
}
}
} int main()
{
int i;
int f,c,root;
while(scanf("%d",&n)!=EOF)
{
tot=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(father,,sizeof(father));
memset(visited,,sizeof(visited));
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
addedge(c,f);
father[c] = f;
if( root == c || beg )
{
root = f;
}
}
while(father[root])//查找父结点
root=father[root];
tree_dp(root);
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ; }

hdu1520 树形dp Anniversary party的更多相关文章

  1. HDU1520 树形DP入门

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. hdu1520 树形dp

    树形dp入门,在树上进行dp. 状态转移方程: dp[i][0] = max(dp[j][0], dp[j][1]);//i为j的上司 并且i不来 dp[i][1] = dp[j][0];//i来了 ...

  3. hdu1520树形dp入门

    题目链接 题意:要开派对,邀请了上司就不能邀请他的下属,邀请了下属就不能邀请他的上司,每个人有一个值,求邀请的人的总值最大 第一行给出一个数n,代表有n个人. 下面n行分别给出n个人的的值 再下面n行 ...

  4. 树形dp Anniversary party(HDU1520)

    题意:给出一棵树,(上下级关系)每个节点都有一个权值,要求选出一些节点满足这些节点任意连个点都不是直接的上下级关系,可以得到的最大权值是多少? 分析:对于每个点有两个状态选或者不选,用状态数组dp[u ...

  5. hdu1520树形dp第一题

    判断最大的欢喜值,如果上司来了,直系下属就不来 如果子节点j不来那么dp[i][1]+=dp[j][0];如果子节点j来那么dp[i][0]+=max(dp[j][0],dp[j][1]);//因为j ...

  6. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  7. HDU1520:Anniversary party(树形dp第一发)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1520 一个公司去参加宴会,要求去的人不能有直接领导关系,给出每一个人的欢乐值,和L K代表K是L的直接领导 ...

  8. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  9. 【树形dp小练】HDU1520 HDU2196 HDU1561 HDU3534

    [树形dp]就是在树上做的一些dp之类的递推,由于一般须要递归处理.因此平庸情况的处理可能须要理清思路.昨晚開始切了4题,作为入门训练.题目都很easy.可是似乎做起来都还口以- hdu1520 An ...

随机推荐

  1. /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 的区别(转)

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个运 ...

  2. Java字节流与字符流基本操作

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...

  3. Codeforces 697A - Pineapple Incident

    题目链接:http://codeforces.com/problemset/problem/697/A 题目大意: 输入三个数 t,s,x; 判断x是否合适 合适的位置位 t , t+s, t+s+1 ...

  4. time模块

    In [1]: import time In [2]: import datetime In [3]: date_time = datetime.datetime.now() In [4]: prin ...

  5. CPU绑定操作

    使用virsh vcpuinfp命令查看虚拟机VCPU和物理CPU的对应关系 [root@svn ~]# virsh vcpuinfo 16 VCPU: 0 CPU: 3 状态: running CP ...

  6. linux标准输入输出及错误输出

    Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...

  7. Orchard源码分析(3):Orchard.WarmupStarter程序集

    概述 Orchard.WarmupStarter程序集包含三个类:WarmupUtility.WarmupHttpModule和Starter<T>.该程序集主要为Orchard应用启动初 ...

  8. ASP.NET MVC使用Bootstrap系列(5)——创建ASP.NET MVC Bootstrap Helpers

    阅读目录 序言 内置的HTML Helpers 创建自定义的Helpers 使用静态方法创建Helpers 使用扩展方法创建Helpers 创建Fluent Helpers 创建自动闭合的Helper ...

  9. #ifdef的用法【转】

    #ifdef的用法     #ifdef的用法灵活使用#ifdef指示符,我们可以区隔一些与特定头文件.程序库和其他文件版本有关的代码.代码举例:新建define.cpp文件 #include &qu ...

  10. the usage of linux command "expect"

    #! /usr/bin/expect -f# this script is used to practise the command "expect" #when "li ...