题目链接: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. 12、Cocos2dx 3.0游戏开发找小三之3.0中的生命周期分析

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27706303 生命周期分析 在前面文章中我们执行了第 ...

  2. WWDC笔记:2013 Session 201 Building User Interfaces for iOS 7

    Text Dynamic Type Specifies fonts semantically Supports user text sizing Optimized for legibility Su ...

  3. c# DataGridView样式设置无效

    对DataGridView中的某些行设置样式时,无效,最后发现,我是先设置完样式再进行展现的this.controls.Add,应该先展现完了,再设置样式.

  4. python 三维坐标图

    绘制3D柱状图,其数据格式为,二维数组或三维数组. from numpy import * file=open('C:\\Users\\jyjh\\Desktop\\count.txt','r') a ...

  5. ubuntu14安装docker-ce

    先卸载旧的docker sudo apt-get remove docker docker-engine docker.io docker-ce 通过HTTPS使用存储库(repository) su ...

  6. A Go library implementing an FST (finite state transducer)——mark下

    https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...

  7. 短链接及关键字过滤ac自动机设计思路

    =============:短链接设计思路:核心:将长字符转为短字符串并建立映射关系,存储redis中.1.使用crc32转换为Long 2.hashids将long encode为最短字符串.作为短 ...

  8. bzoj 2199: [Usaco2011 Jan]奶牛议会【2-SAT】

    好久没写2-SAT了啊,还以为是网络流 设点x为选,x'为不选,因为一头牛至少要满足一个条件,所以对于牛条件的两个点,选了一个不符合的点,就要选另一个符合的点,这样连两条边 然后枚举所有议案的选和不选 ...

  9. [SDOI2010]外星千足虫(高斯消元)

    高斯消元裸题... 方法一:暴力,O(2^n)20分 方法二:直接Gauss,加点玄学技巧搞得好的话70分 方法三:使用bitset优化,复杂度:$O(\frac{n^3}{ω})$ 不会的同学看一下 ...

  10. C#中接受一个非字符串的输入

    接受来自用户的值 System 命名空间中的 Console 类提供了一个函数 ReadLine(),用于接收来自用户的输入,并把它存储到一个变量中. 例如: int num; num = Conve ...