题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520

Anniversary party

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

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
 
看题解说是树型dp,就学习了一下。 
状态转移:
  上司去,下级不去:

    dp[node][1]+=dp[u][0];      //node去,则u必不能去

  上司不去,下级去或者不去:
    dp[node][0]+=max(dp[u][0],dp[u][1]);      //node不去,取u去或不去的最大值

和深搜很像,也就是用的深搜。

hdu上的数据更加刁钻,poj上能过的代码,在hdu上又是wa又是TLE。

开始模仿别人的代码用数组存上司和下级的关系,一直超时,最后改为用vector做.有的题解说可能不只一棵树,但是题目上说了是一棵树。

用了二维的vector,注意,每一组样例,都要清空vector,二维的需要每一维clear。用vector来搜索非常方便,节约时间和空间。

本题中vis数组没用。

注意理解树型dp思想。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<vector>
#include<iostream>
using namespace std; int father[],vis[],dp[][],t;
vector<int> v[];
void dfs(int node)
{
int i,j;
vis[node] = ;
for(i = ; i<v[node].size(); i++)
{
int u=v[node][i];
//if(!vis[u])
//{
dfs(u);
dp[node][]+=dp[u][];//node去,则i必不能去
dp[node][]+=max(dp[u][],dp[u][]);//node不去,取i去或不去的最大值
//}
}
} int main()
{
int i,j,l,k,root; while(~scanf("%d",&t))
{
//memset(father,0,sizeof(father)); for(i = ; i<=t; i++)
{
v[i].clear();
scanf("%d",&dp[i][]);
dp[i][]=;
father[i]=;
}
while(scanf("%d%d",&l,&k),l+k>)
{
v[k].push_back(l);
father[l]++;
}
//father[l] = k;//记录上司
int ans=;
for(int i=; i<=t; i++)
if(father[i]==)
{
memset(vis,,sizeof(vis));
dfs(i);
ans+=max(dp[i][],dp[i][]);
}
printf("%d\n",ans);
}
return ;
}

HDU_1520_Anniversary party_树型dp的更多相关文章

  1. POJ3659 Cell Phone Network(树上最小支配集:树型DP)

    题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...

  2. POJ 3342 - Party at Hali-Bula 树型DP+最优解唯一性判断

    好久没写树型dp了...以前都是先找到叶子节点.用队列维护来做的...这次学着vector动态数组+DFS回朔的方法..感觉思路更加的清晰... 关于题目的第一问...能邀请到的最多人数..so ea ...

  3. 【XSY1905】【XSY2761】新访问计划 二分 树型DP

    题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...

  4. 洛谷P3354 Riv河流 [IOI2005] 树型dp

    正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...

  5. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  6. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...

  7. ZOJ 3949 (17th 浙大校赛 B题,树型DP)

    题目链接  The 17th Zhejiang University Programming Contest Problem B 题意  给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...

  8. BZOJ 1564 :[NOI2009]二叉查找树(树型DP)

    二叉查找树 [题目描述] 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子结点的数据值小. 另一方面,这棵查找树中每个结点都有一个权值,每个结 ...

  9. Codeforces 149D Coloring Brackets(树型DP)

    题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...

随机推荐

  1. Java基础笔记(一)

    本文主要是我在看<疯狂Java讲义>时的读书笔记,阅读的比较仓促,就用 markdown 写了个概要. 第一章 Java概述 Java SE:(Java Platform, Standar ...

  2. Cocos2d-X中的ProgressTimer

     ProgressTimer即进度条,进度条在游戏开发中运用很广泛,比如在一些格斗游戏中,显示血液的变化,还有游戏载入进度,等都离不开进度条 Cocos2d-X中使用CCProgressTimer ...

  3. CListCtrl在处理NM_CLICK的时候,怎样推断Ctrl SHIFT ALT键是否按下

    百度 NMITEMACTIVATE 在 NM_CLICK  的处理函数中.将 NMHDR 结构强制转换成 NMITEMACTIVATE.这一步MS已经帮助我们完毕了,当中,结构体中的 uKeyFlag ...

  4. vim note(5)

    .vimrc 的设置 $HOME/.vimrc 的普通设置,例如以下. set nocompatible "" not compatible with VI "" ...

  5. opencl+opencv实现sobel算法

    这几天在看opencl编程指南.照着书中的样例实现了sobel算法: 1.结合opencv读取图像,保存到缓冲区中. 2.编写和编译内核.并保存显示处理后的结果. 内核: const sampler_ ...

  6. python的一些常用函数

    1 filter(function, iterable) 等价于(item for item in iterable if function(item)) 就是说,filter会遍历iterable中 ...

  7. SoapUI中的RegEx

    在for Content matching RegEx中

  8. 8-12 canvas专题-阶段练习一(上)

    8-12 canvas专题-阶段练习一(上) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...

  9. BZOJ_3133_[Baltic2013]ballmachine_堆+倍增

    BZOJ_3133_[Baltic2013]ballmachine_堆+倍增 Description 有一个装球机器,构造可以看作是一棵树.有下面两种操作: 从根放入一个球,只要下方有空位,球会沿着树 ...

  10. linux线程相关函数接口

    以下内容转自网络 索引:1.创建线程pthread_create2.等待线程结束pthread_join3.分离线程pthread_detach4.创建线程键pthread_key_create5.删 ...