Anniversary party

HDU - 1520

题意:你要举行一个晚会,所有人的关系可以构成一棵树,要求上下级关系的人不能同时出现,每一个人都有一个rating值,要求使整个晚会的rating值最大。

/*
解题思路:树形dp,随意从一个点开始扩展,把周围所有节点的dp都解决出来,然后加上去即可。
用dp[i][0]表示不选中i号,周围的都可以选的最大值,dp[i][1]表示选中i号,那么周围都不能选的最大值。

dp[i][0]+=(dp[j][1],dp[j][0])
dp[i][1]+=dp[j][0]
i和j相邻,并且在求i的时候从j扩展的都已经求出来。
*/
#include<cstdio>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
#define Max(a,b) a>b?a:b
vector<int> q[];
int visit[],rating[],dp[][];
void dfs(int r)
{
//printf("r=%d\n",r);
int i,v;
visit[r]=;
if(q[r].size()==)
{
dp[r][]=rating[r];
dp[r][]=;
}
int dp1=,dp0=;
for(i=;i<q[r].size();i++)
{
v=q[r][i];
if(!visit[v])
{
//printf("v=%d ",v);
dfs(v);
}
dp1+=dp[v][];//若选了此节点,则孩子都不能选
dp0+=Max(dp[v][],dp[v][]);//若不选此节点,则孩子可选可不选
}
dp[r][]=dp1+rating[r];
dp[r][]=dp0;
}
int main()
{
int n,root;
int i,j,k;
while(~scanf("%d",&n))
{
for(i=;i<=n;i++)
q[i].clear();
for(i=;i<=n;i++)
scanf("%d",&rating[i]);
memset(visit,,sizeof(visit));
int c,f;
root=;
while(scanf("%d%d",&c,&f),c||f)
{
if(c==root)
root=f;//新学习的一个寻根的小技巧
q[f].push_back(c);
}
dfs(root);
printf("%d\n",Max(dp[root][],dp[root][]));
}
return ;
}

hdu1520 Anniversary party的更多相关文章

  1. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  2. HDU1520 Anniversary party —— 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  3. hdu1520 Anniversary party (树形dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...

  4. HDU-1520 Anniversary party(树形DP)

    题目大意:一棵树,每个节点都带权.从中取出一些节点,并且子节点不能与父节点同时取,求能取得的最大值. 题目分析:定义状态dp(u,0/1)表示u点不取/取.则状态转移方程为: dp(u,1)=sum( ...

  5. hdu1520 Anniversary party 简单树形DP

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

  6. 树状DP HDU1520 Anniversary party

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...

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

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

  8. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. Anniversary party(hdu1520)

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

随机推荐

  1. Java for LeetCode 129 Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. Maven项目中使用JUnit进行单元测试

    1.打开maven项目中的pom.xml,添加JUnit 的jar包 2.在src/test/java下右键新建JUnit Test Cast

  3. 查 101.201.62.30 IP信誉方法

    查 101.201.62.30 IP信誉方法https://www.virustotal.com/#/ip-address/101.201.62.30https://talosintelligence ...

  4. Elasticsearch mapping文档相似性算法

    Elasticsearch allows you to configure a scoring algorithm or similarity per field. The similarityset ...

  5. C语言实现wc基本功能

    GitHub地址:https://github.com/hhx007/wc 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数. 这个项目要求写一个命令行程序,模仿已有w ...

  6. darknet YOLOv2安装及数据集训练

    一. YOLOv2安装使用 1. darknet YOLOv2安装 git clone https://github.com/pjreddie/darknetcd darknetmake或到网址上下载 ...

  7. 1087 All Roads Lead to Rome (30)(30 分)

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  8. 千万别用MongoDB?真的吗?!

    某人发了一篇Don’t use MongoDB的血泪控诉,我把原文翻译如下,你可以看看.不过,我想我们还要去看看10gen CTO的对此事的回复,我们还要去在Reddit上看看大家的说法,10gen  ...

  9. ogg概叙、架构、进程

    一. OGG 概述 OGG 全称Oracle Golden Gate. 历史: Golden Gate公司于1995年成立于美国加州旧金山,它的名称源自旧金山闻名于世的金门大桥.两位创始人Eric F ...

  10. jquery.one()

    one() 方法为被选元素附加一个或多个事件处理程序,并规定当事件发生时运行的函数. 当使用 one() 方法时,每个元素只能运行一次事件处理器函数. 也就是,绑定的function,只会执行一次. ...