Anniversary party

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

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

//题解就不写了 看代码就应该懂了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
vector<int>vec[maxn];
int value[maxn],vis[maxn],f[maxn][],in[maxn];
void dfs(int u){
   f[u][]=;
   f[u][]=value[u];
   vis[u]=;
   for(int i=;i<(int)vec[u].size();++i){
    int v=vec[u][i];
    if(vis[v]) continue;
    dfs(v);
    f[u][]+=max(f[v][],f[v][]);
    f[u][]+=f[v][];
   }
   return ;
}
int main(){
    int n,u,v,k;
    while(~scanf("%d",&n)){
        for(int i=;i<=n;++i) vec[i].clear();
        for(int i=;i<=n;++i) scanf("%d",&value[i]);
        memset(in,,sizeof(in));
        while(scanf("%d%d",&v,&u),u||v){
            vec[u].push_back(v);
            ++in[v];
        }
        for(int i=;i<=n;++i){
            if(!in[i]) {
                memset(vis,,sizeof(vis));
                memset(f,,sizeof(f));
                dfs(i);
                printf("%d\n",max(f[i][],f[i][]));
                break;
            }
        }
    }
}

树形DP基础题 HDU1520的更多相关文章

  1. POJ 2342 Anniversary party 树形DP基础题

    题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...

  2. POJ 1155 TELE 背包型树形DP 经典题

    由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...

  3. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  4. 51nod 1353 树 | 树形DP经典题!

    51nod 1353 树 | 树形DP好题! 题面 切断一棵树的任意条边,这棵树会变成一棵森林. 现要求森林中每棵树的节点个数不小于k,求有多少种切法. 数据范围:\(n \le 2000\). 题解 ...

  5. hdu 2089 不要62 (数位dp基础题)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  7. P2016 战略游戏——树形DP大水题

    P2016 战略游戏 树形DP 入门题吧(现在怎么是蓝色标签搞不懂): 注意是看见每一条边而不是每一个点(因为这里错了好几次): #include<cstdio> #include< ...

  8. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  9. 树形dp基础

    今天来给大家讲一下数形dp基础 树形dp常与树上问题(lca.直径.重心)结合起来 而这里只讲最最基础的树上dp 1.选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程 ...

随机推荐

  1. Linux网络管理员:网络概论

    1.TCP/IP网络 包是适合通过网络传输的一小段数据,交换发生于网络的每一个链接点.当不同来源的包必须经过同一条线路传输数据时,这些包将被交替传输. 2.TCP/IP协议组 TCP/IP协议簇是In ...

  2. FluentAspects -- 基于 Fluent API 的 Aop

    FluentAspects -- 基于 Fluent API 的 Aop Intro 上次我们做了一个简单的 AOP 实现示例,但是实现起来主要是基于 Attribute 来做的,对于代码的侵入性太强 ...

  3. 初篇:我与Linux

        据悉,红帽认证将于本年的8月份更换Rhel7为Rhel8.所以我想趁这次机会搏一搏.     我个人是初中就神仰Linux已久,只不过那个时候的我只知道Linux系统,不知道有什么区分.奈何那 ...

  4. 7.哪些工具可以帮助查找bug或进行静态分析

    哪些工具可以帮助查找bug或进行静态分析? PyChecker is a static analysis tool that detects the bugs in Python source cod ...

  5. yum报[Errno 256] No more mirrors to try

    解决方法: yum clean all            #清除yum缓存yum makecache      #将服务器软件包写到本地缓存,提高包的搜索.安装效率

  6. 记坑: ConfigurationProperties 和 RefreshScope

    为什么80%的码农都做不了架构师?>>>   当使用spring-cloud-config 和 spring-boot-configuration-processor时 使用bean ...

  7. Highcharts的自适应DOM或者DIV,JS方法实现

    那我们就按照官网的一分钟极速入门代码来说 // 图表配置 var options = { chart: { type: 'bar' //指定图表的类型,默认是折线图(line) }, title: { ...

  8. 与IBM的Lin Sun关于Istio 1.0和微服务的问答

    北京时间 7 月 31 日,Istio 正式发布了 1.0 版本,并表示已经可用于生产环境.该版本的主要新特性包括跨集群 mesh 支持.细粒度流量控制以及在一个 mesh 中增量推出 mutual ...

  9. 修复.NET的HttpClient

    \ 看新闻很累?看技术新闻更累?试试下载InfoQ手机客户端,每天上下班路上听新闻,有趣还有料! \ \\ 早在2016年我们就报道过 ,.NET的HttpClient存在一些问题.随着.NET Co ...

  10. YUM 安装lnmy

    yum -y install nginx systemctl start nginx.service yum -y install php php-fpm php-mysql php-gd libjp ...