HDOJ 题目1520 Anniversary party(树形dp)
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6271 Accepted Submission(s): 2820
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.
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
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
- #include<stdio.h>
- #include<string.h>
- #define max(a,b) (a>b?
- a:b)
- struct s
- {
- int u,v,next;
- }edge[3000100];
- int a[6060],dp[6060][2],dig[6060],head[6060],vis[6060],cnt;
- void add(int u,int v)
- {
- edge[cnt].u=u;
- edge[cnt].v=v;
- edge[cnt].next=head[u];
- head[u]=cnt++;
- }
- void dfs(int u)
- {
- vis[u]=1;
- dp[u][1]=a[u];
- int i;
- for(i=head[u];i!=-1;i=edge[i].next)
- {
- int v=edge[i].v;
- if(!vis[v])
- {
- dfs(v);
- dp[u][0]+=max(dp[v][0],dp[v][1]);
- dp[u][1]+=dp[v][0];
- }
- }
- }
- int main()
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- int i;
- for(i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- }
- int u,v;
- cnt=0;
- memset(head,-1,sizeof(head));
- memset(dp,0,sizeof(dp));
- memset(dig,0,sizeof(dig));
- memset(vis,0,sizeof(vis));
- while(scanf("%d%d",&v,&u),u||v)
- {
- add(u,v);
- dig[v]++;
- }
- int s;
- for(i=1;i<=n;i++)
- {
- if(!dig[i])
- {
- s=i;
- }
- }
- dfs(s);
- printf("%d\n",max(dp[s][0],dp[s][1]));
- }
- }
HDOJ 题目1520 Anniversary party(树形dp)的更多相关文章
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- hdu oj 1520 Anniversary party(树形dp入门)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- [poj2342]Anniversary party_树形dp
Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- HDU1520 Anniversary party —— 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 【HDOJ】1520 Anniversary party
第二道树形DP,先是MLE.后来仅需改小邻接矩阵的第二个维度到30就过了. #include <cstdio> #include <cstring> #include < ...
随机推荐
- laydate 监听日期切换
```` //日期范围 laydate.render({ elem: '#Time', range: "至", max: gitData() ,done: function(val ...
- python_for循环
#for循环'''for i in range(0,10,2):age_oldboy = 56for i in range(3): guess_age = int(input("guess ...
- 【【henuacm2016级暑期训练】动态规划专题 L】Civilization
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给一个森林. 就是由很多棵树组成.. 然后会询问你其中一棵树的最长链. 初始状态的最长链可以用两遍dfs分别找最长路得到. 然后要求 ...
- MyBatis学习总结(2)——使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- FastDFS 实现图片上传_01
一.jar 包 jar包下载:https://pan.baidu.com/s/1nwkAHU5 密码:tlv6 或者 下载工程,安装到 maven 本地仓库 工程下载:https://pan.baid ...
- linux下使用DBCA(database configuration assistant)创建oracle数据库
前提:切换到图形界面 到Oracle的bin文件夹下,使用oracle用户.运行dbca就可以.和windows的效果一样. 假设出现乱码 export LANG="en_US:UTF-8& ...
- Django连接mysql
链接文档地址:https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial02/ 由于我使用的是mysql,所以设置的是mysql的: 在mysl ...
- BZOJ 1007: [HNOI2008]水平可见直线 平面直线
1007: [HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则 ...
- No connection could be made because the target machine actively refused it [::1]:808
No connection could be made because the target machine actively refused it [::1]:808 1.首先查看端口占用情况, 在 ...
- zzulioj--1791-- 旋转矩阵(模拟水题)
旋转矩阵 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 268 Solved: 116 SubmitStatusWeb Board Descr ...