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. Linux下Memcache 安装和使用

    Memcached是一种高性能的分布式内存对象缓存系统(memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能,其“分布式”由客户端函数库完成,成熟算法的为一致性Hash),用 ...

  2. Cache-Aside Pattern解析

    使用这种模式,可以帮助我们维护Cache中的数据. 使用Cache容易遇到的问题: 使用缓存,主要是为了将一些重复访问的数据存到缓存,开发者希望缓存中的数据和数据源中的保持一致,这就需要程序中有相应的 ...

  3. ES6标准

    1. ES6标准感觉越来越向传统语言靠拢了,以后写到ES6的标准都记录下: ,,]; // =>操作符 array.forEach(v => console.log(v)); 是不是简化了 ...

  4. Ftp软件

    http://www.xlightftpd.com/cn/index.htm FileZilla Server官网:http://www.filezilla-project.org/

  5. ProFTPD <=1.3.5 mod_copy 未授权文件复制漏洞

    poc如下: #!/usr/bin/env python# coding=utf-8 """Site: http://www.beebeeto.com/Framework ...

  6. C-结构体、枚举

    #include <stdio.h> //结构体:可以由多个不同类型的数据构成 int main() { struct Person { //里面的3个变量,可以称为是结构体的成员或者属性 ...

  7. mysql max_allowed_packet查询和修改

    http://www.2cto.com/database/201303/195830.html mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会被max_allowe ...

  8. Vue 入门指南 JS

    Vue 入门指南 章节导航 英文:http://vuejs.org/guide/index.html 介绍 vue.js 是用来构建web应用接口的一个库 技术上,Vue.js 重点集中在MVVM模式 ...

  9. PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系

    看PostgreSQL9的官方文档,我越看越迷糊,这表空间,数据库,模式,表,用户,角色之间的关系怎么在PostgreSQL里这么混乱呢?经过中午的一个小实验,我逐渐理清了个中来龙去脉.下面我来还原我 ...

  10. linux程序设计1

    a.out 的意思是 assembler output,即汇编输出. C语言的头文件一般位于 /usr/include/ 目录下,而依赖于特定 Linux 版本的头文件通常可在目录 /usr/incl ...