题目链接: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. 可利用空间表(Free List)

    写这篇文章的动因是因为 2015 年 04 月 02 日的阿里在线笔试题考到了这个知识点.我当时模模糊糊的写了一些,估计写的也不对,所以在这里总结一下. 原题 常常会有频繁申请.释放内存的需求,比如在 ...

  2. Cocos2dx--开发环境搭建

    配置文档下载:http://pan.baidu.com/s/1rja8I 有些文件比較大的自己去下,这里列一下全部用到的文件 adt-bundle-windows-x86-20140321.zip a ...

  3. 编译android的一些坑

    1 降级gcc g++到4.4 2 参考:http://source.android.com/source/initializing.html来配置环境 3 使用jdk1.6 包括 java java ...

  4. bzoj1050

    最小生成树 其实这道题是最小生成树的变种,我们发现答案不一定在最小/最大生成树上,最短路算法也不可行,因为我们我们并不是希望最小值尽量的大,最大值尽量的小,这样不一定是最优的,那么我们枚举最小的边,然 ...

  5. IntelliJ IDEA Tomcat配置

    解决方法: 下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/native

  6. 最短路径(Dijkstra算法)

    当用图结构来表示通信.交通等网络,权重代表距离或者成本,寻找最短路径就成为了一个重要的任务. 给定带权网络G=(V;E),源点s,对于其他所有顶点v,寻找s到v的最短路径,连接成一颗最短路径树.可以证 ...

  7. bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】

    至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...

  8. SpringCloud(Finchley版本)中Zull过滤器ResponseBoby返回中文乱码解决方案

    Spring Cloud带有"Cloud"的字样,但它并不是云计算解决方案,而是在Spring Boot基础上构建的,用于快速构建分布式系统的通用模式的工具集.使用Spring C ...

  9. eccharts-gl 3D立体柱状图

    echarts-gl继承于echarts echarts-gl官方实例https://echarts.baidu.com/examples/index.html#chart-type-globe 代码 ...

  10. 【杂文】虚拟键码表(供函数GetAsyncKeyState()使用)

    [杂文]虚拟键码表(供函数GetAsyncKeyState()使用) 什么?你说你不知道 \(GetAsyncKeyState()\) ?\(→\)戳我\(←\) (惊奇的发现 \(0\) ~ \(9 ...