DP:树DP
The more, The Better
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5414 Accepted Submission(s): 3217
b 代表第 i 个城堡的宝物数量, b >= 0。当N = 0, M = 0输入结束。
3 2
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0
5dp[i][j]表示以i为根节点j个子节点的最大值。
13#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
typedef long long LL;
using namespace std;
const int maxn=220;
int v[maxn];
int n,m;
int dp[maxn][maxn];
vector<int>s[maxn];
void tree_dp(int n,int f)
{
int len=s[n].size();
dp[n][1]=v[n];
for(int i=0;i<len;i++)
{
if(f>1) tree_dp(s[n][i],f-1);
for(int j=f;j>=1;j--)
{
for(int k=1;k<=j;k++)
dp[n][j+1]=max(dp[n][j+1],dp[n][j+1-k]+dp[s[n][i]][k]);
}
}
}
int main()
{
int f;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
v[0]=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++)
s[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&f,&v[i]);
s[f].push_back(i);
}
tree_dp(0,m+1);
printf("%d\n",dp[0][m+1]);
}
return 0;
}Anniversary party
Time
Limit: 1000MSMemory Limit: 65536K Total Submissions: 4329 Accepted: 2463 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 N – 1 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 0Output
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 0Sample Output
5#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=6005;
int dp[maxn][2],pre[maxn];
int visit[maxn],n;
void tree_dp(int x)
{
visit[x]=1;
for(int i=1;i<=n;i++)
{
// cout<<"111 "<<i<<endl;
if(!visit[i]&&pre[i]==x)
{
tree_dp(i);
dp[x][1]+=dp[i][0];
dp[x][0]+=max(dp[i][1],dp[i][0]);
}
}
} int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
memset(visit,0,sizeof(visit));
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)
scanf("%d",&dp[i][1]);
int x,y,root;
while(~scanf("%d%d",&x,&y)&&(x+y))
{
pre[x]=y;
root=y;
}
while(pre[root])
root=pre[root];
// cout<<"fuck "<<root<<endl;
tree_dp(root);
printf("%d\n",max(dp[root][0],dp[root][1]));
}
return 0;
}
DP:树DP的更多相关文章
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- hdu 1520 Anniversary party 基础树dp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 【BZOJ】1040: [ZJOI2008]骑士(环套树dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 简直不能再神的题orz. 蒟蒻即使蒟蒻,完全不会. 一开始看到数据n<=1000000就 ...
- [51NOD1405] 树的距离之和(树DP)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- [HDOJ2196]Computer (树直径, 树DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 给一棵树,求树上各点到某点的距离中最长的距离.注意每个点都要求. 和普通求树的直径不一样,要求每 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
随机推荐
- 每天一个JavaScript实例-动态省份选择城市
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- oracle 在操作blob该字段是否会产生很多redo
操作blob该字段是否会产生很多redo,答案是否定的.以下来做一个实验,測试数据库版本号是11.2.0.1.0: --创建一张表做測试之用 create table test_blob ( id ...
- IP Editor IP控件(对比一下封装IP控件)
HWND hIpEdit; void __fastcall TForm2::FormCreate(TObject *Sender) { hIpEdit = CreateWindow(WC_IPADDR ...
- Iconfinder 如何杜绝盗版,哈希算法检测图像重复
原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...
- leetcode回文子串拆分-最小拆分次数
转载请注明来自souldak,微博:@evagle 上一篇是要输出所有的可能拆分,这回是要输出拆分次数最少的切割次数. 如果直接按照上一篇那么做的话,就会超时,因为我们在判断s[i][j]是否是回文的 ...
- hdoj 1258 SUM IT UP
程序的思想是:输入数据是,先使用快排对其从大到小进行排序,然后记录相同数据的个数,比如4 3 3 2 2 1 1,最后的数据变成4 3 2 1 ,并且同时数据的个数f[]变成1 2 2 2 然后就是遍 ...
- ExtJS学习--------Ext.Element中的经常使用事件和其它重要的方法学习(实例)
经常使用事件: 其它重要方法: 详细实例:(实例结果能够将相应的代码取消凝视进行測试) Ext.onReady(function(){ Ext.create('Ext.panel.Panel',{ t ...
- 苹果iOS手机系统诊断功能是后门吗?
7月20日,美国知名苹果iOS手机系统侦破专家扎德尔斯基在2014年世界黑客大会(HOPE/X)用幻灯片讲演揭露了苹果手机存在系统级"后门". 为此,7月23日.苹果公司马上做出回 ...
- 基于Java的开源CMS系统选择(转)
CMS概述 对于网站CMS系统而言,基于PHP的是主流,如Drupal/Joomla在各个主流虚拟机提供商上都是标准配置,也被广泛使用. 但如果你拥有Java团队,或者项目目标是想建立一个企业网使用的 ...
- windows server 搭建radius服务器
使用ISA 2004搭建PPTP/L2TP 服务器后,VPN账号是在radius服务器上的,使用window server 2003搭建radius服务器,需要添加组件->internet验证服 ...