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. Android学习笔记——Handler(一)

    使用Handler管理线程(转) 步骤: 1. 申请一个Handler对象 Handler handler = new Handler(); 2. 创建一个线程 {继承Thread类或者实现Runna ...

  2. ecshop 后台模板设置-》设置模板

    ecshop后台“设置模板”出现问题 问题1:不能出现特殊符号  / <!-- TemplateBeginEditable name="5F生活数码/手机" -->&l ...

  3. css媒体查询

    简单解释:http://zh.learnlayout.com/media-queries.html 深入学习1:https://developer.mozilla.org/en-US/docs/Web ...

  4. JavaScript中原型和原型链

    原型[prototype]: 为其他对象提供共享属性的对象. 每个函数都有一个原型(prototype)属性,这个属性是一个指针,指向一个对象,这个对象包含特定实例共享的一些属性和方法. 以例服人: ...

  5. mysql之用户授权

    授权命令: 1.全部权限:grant all on *.* to user@192.168.10.2 identified by "pass": 2.部分权限:grant sele ...

  6. C#和SQL实现的字符串相似度计算代码分享

    http://www.jb51.net/article/55941.htm C#实现: 复制代码 代码如下: #region 计算字符串相似度        /// <summary>   ...

  7. git diff

    git diff  工作区与暂存区的差别 git diff -cached / git diff -staged  暂存区与版本库的差别 git diff HEAD  工作区与版本库的差别 git d ...

  8. Mac截图快捷键

    Shift+Command+3 截取全屏幕至桌面 Shift+Command+4 截取部分屏幕至桌面 Shift+Command+4+空格 截取窗口或原件至桌面 Shift+Command+4 然后E ...

  9. java中跳出if判断

    今天学到的一点儿新东西一个if判断里面有好多东西,紧接着还有其他代码,不能使用return来结束这个if判断这时候,就需要这样: out:if (!"null".equals(re ...

  10. 点击每个li输出里面的内容(前端很常问的面试题之一)

    点击每个li输出里面的内容(前端很常问的面试题之一) 前端 面试 JavaScript <!DOCTYPE html> <html lang="en"> & ...