hdu1520:树形dp

被坑。。。hdu都是多组数据读入的

----------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
 int x=0;
 char c=getchar();
 int f=1;
 while(!isdigit(c)){
  if(c==‘-‘) f=-1;
  c=getchar();
 }
 while(isdigit(c)){
  x=x*10+c-‘0‘;
  c=getchar();
 }
 return x*f;
}
vector<int>e[6005];
int w[6005],v[6005],f[6005][2];
void dp(int x){
 for(int i=0;i<e[x].size();i++){
  dp(e[x][i]);
  f[x][1]+=f[e[x][i]][0];
  f[x][0]+=max(f[e[x][i]][0],f[e[x][i]][1]);
 }
 f[x][1]+=w[x];
 return ;
}
int main(){
 int n;
 while(scanf("%d",&n)==1){
  rep(i,n) e[i].clear();
  clr(v,0);clr(f,0);
 rep(i,n){
  w[i]=read();
 }
 int s,t;
 while(scanf("%d%d",&s,&t)==2&&s&&t){
  e[t].push_back(s);
  v[s]=1;
 }
 rep(i,n) if(v[i]==0) s=i;
 /*rep(i,n) {
  printf("%d\n",i);
  for(int j=0;j<e[i].size();j++)
    printf("%d ",e[i][j]);
  printf("\n");
 }
 printf("%d\n",s);*/
 dp(s);
 printf("%d\n",max(f[s][0],f[s][1]));}
 return 0;
}

----------------------------------------------------------------------------------------------

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7559    Accepted Submission(s): 3315

Problem 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

Source

Ural State University Internal Contest October‘2000 Students Session

Recommend

linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242

时间: 2024-10-08 05:36:22

hdu1520:树形dp的相关文章

hdu1520树形dp入门

题目链接 题意:要开派对,邀请了上司就不能邀请他的下属,邀请了下属就不能邀请他的上司,每个人有一个值,求邀请的人的总值最大 第一行给出一个数n,代表有n个人. 下面n行分别给出n个人的的值 再下面n行每行给出L,K;K是L的上司 以0 0结束一组输入 树形dp:把每个人看成一个点,则该点有两个状态:邀请或没被邀请 定义f[u][0]为节点没被邀请时的值:f[u][1]为节点被邀请时的值 状态转移方程: f[u][0]=sum(max(f[v][0],f[v][1])//v为u的下属 f[u][1

HDU1520(树形dp)

H - Anniversary party Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical

HDU1520——树形DP——Anniversary party

Problem 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.

HDU-1520 树形dp

Problem 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.

hdu1520树形dp第一题

判断最大的欢喜值,如果上司来了,直系下属就不来 如果子节点j不来那么dp[i][1]+=dp[j][0];如果子节点j来那么dp[i][0]+=max(dp[j][0],dp[j][1]);//因为j不来i也可以不来 递归的求子节点值 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include

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

A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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

【树形dp小练】HDU1520 HDU2196 HDU1561 HDU3534

[树形dp]就是在树上做的一些dp之类的递推,因为一般需要递归处理,因此平凡情况的处理可能需要理清思路.昨晚开始切了4题,作为入门训练.题目都非常简单,但是似乎做起来都还口以- hdu1520 Anniversary party 给一颗关系树,各点有权值,选一些点出来.任一对直接相连的边连接的点不能同时选择,问选择出的权值和最大为多少. 考虑以u为根的子树可以选择的方法,dp[u]表示选择u时能获得最大收益,dp2[u]表示不选u时能获得的最大收益.则u不选时,为dp2[u]=max{dp[v]

hdu1520 (树形dp)

hdu1520 http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意是给定一棵树,每个结点有一个价值,要我们选择任意个结点使得总价值最大,规则是如果父亲结点被选了,那么儿子结点不可以被选,但是儿子的儿子可以被选 本来学搜索的时候找到这题搜索题,然后用搜索做的 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <algo

HDU-1520 Anniversary party (树形DP)

Problem 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.

hdu1520 Anniversary party(最大独立集 树形dp)

题目链接:点击打开链接 题目描述:现有一棵树,树上每个结点都有一个权值,问从中选一些点,这些点两两之间不直接连接,问权值最大为多少? 解题思路:很裸的一道树上最大独立集问题 树形dp即可 dp[i][0]:不选i节点 dp[i][0]+=max(dp[t][0],dp[t][1]); dp[i][1]:选i节点     dp[i][1]+=dp[t][0]; 代码: #pragma comment(linker,"/STACK:1024000000,1024000000") #incl