题目链接:https://cn.vjudge.net/contest/277955#problem/A

题目大意:略

具体思路:刚开始接触树形dp,说一下我对这个题的初步理解吧,首先,我们从根节点开始,往下dfs,dp[i][0]代表我当前的i点不要去舞会,那么对于他的孩子节点,我们是肯定不能去舞会的,所以dp[i][0]=dp[i][0]+max(dp[son][0],dp[son][1])(注意一个上司可能有多个下属,所以需要累加),这个的具体意思是如果当前的父亲节点不去的话,我们可以选择他的子节点去或者不去都行,我们只需要求出一个最大的加上就可以了。dp[i][1]代表当前的i节点,我们选择去参加舞会,所以他的子节点就肯定去不了了。所以dp[i][1]=dp[i][1]+dp[son][1]. (这道题是多组输入,竟然还卡vector)

AC代码:

 #include<iostream>
#include<cmath>
#include<stack>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = +;
int dp[maxn][],cost[maxn],num;
int father[maxn],head[maxn];
struct node
{
int to;
int nex;
node() {}
node(int xx,int yy)
{
to=xx;
nex=yy;
}
} edge[maxn];
void addedge(int fr,int to){
edge[num].to=to;
edge[num].nex=head[fr];
head[fr]=num++;
}
void dfs(int t)
{
dp[t][]=;
dp[t][]=cost[t];
for(int i=head[t]; i!=-; i=edge[i].nex)
{
int u=edge[i].to;
dfs(u);
dp[t][]+=dp[u][];
dp[t][]+=max(dp[u][],dp[u][]);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
num=;
for(int i=; i<=n; i++)
{
head[i]=-;
father[i]=i;
scanf("%d",&cost[i]);
}
int t1,t2;
for(int i=; i<=n; i++)
{
scanf("%d %d",&t1,&t2);
if(t1==&&t2==)
break;
addedge(t2,t1);
father[t1]=t2;
}
int ed=;
while(ed!=father[ed])
ed=father[ed];
dfs(ed);
printf("%d\n",max(dp[ed][],dp[ed][]));
}
return ;
}

树形dp(A - Anniversary party HDU - 1520 )的更多相关文章

  1. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  2. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  3. 树形dp poj2342 Anniversary party * 求最大价值

    Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State Univer ...

  4. 树形DP +01背包(HDU 1011)

    题意:有n个房间,有n-1条道路连接着n个房间,每个房间都有若干个野怪和一定的能量值,有m个士兵从1房间入口进去,到达每个房间必须要留下若干士兵杀死所有的野怪,然后其他人继续走,(一个士兵可以杀死20 ...

  5. 树形dp(B - Computer HDU - 2196 )

    题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...

  6. HDU 1520.Anniversary party 基础的树形dp

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

  7. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  8. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  9. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

随机推荐

  1. es6 let关键字

    1.let关键字 var arr = [ ]; for(var i=0; i<10; i++){ arr [i] = function(){ alert(i) } } arr [8](); // ...

  2. Office/Visio/Project 2010 RTM (x86) (x64)(中文简体/英文)

    1.cn_office_professional_plus_2010_x64_515528.exe ed2k://|file|cn_office_professional_plus_2010_x64_ ...

  3. Find non-overlap jobs with max cost

    Given a set of n jobs with [start time, end time, cost] find a subset so that no 2 jobs overlap and ...

  4. Day22-Django之缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  5. 关于UIInterfaceOrientation的一个bug

    在ios中获取设备当前方向的枚举有UIInterfaceOrientation和UIDeviceOrientation ,前者包含枚举 Unknown//未知 Portrait//屏幕竖直,home键 ...

  6. [UVA 10635] Prince ans Princess

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...

  7. bzoj 2428: [HAOI2006]均分数据 && bzoj 3680 : 吊打XXX 模拟退火

    每次把元素随便扔随机一个初始解,退火时每次随机拿一个元素扔到随机一个集合里,当温度高时因为状态不稳定扔到那个元素和最小的里边. 如果新解优,更新ans. 把原式拆一下,就可以用int存了. bzoj ...

  8. D. Monitor Educational Codeforces Round 28

    http://codeforces.com/contest/846/problem/D 二分答案 适合于: 判断在t时候第一次成立 哪个状态是最小代价 #include <cstdio> ...

  9. 简例 - robot case格式

    注意robot的case格式 # *** Variables *** # *** Settings *** # Library String # Library Selenium2Library ** ...

  10. bzoj千题计划154:bzoj3343: 教主的魔法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3343 high记录原始身高 HIGH记录每块排序之后的身高 不满一块的直接对high操作,重排之后再 ...