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

Problem Description
ACboy非常喜欢玩一种战略游戏,在一个地图上,有N座城堡。每座城堡都有一定的宝物,在每次游戏中ACboy同意攻克M个城堡并获得里面的宝物。但因为地理位置原因。有些城堡不能直接攻克,要攻克这些城堡必须先攻克其它某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗?
 
Input
每一个測试实例首先包含2个整数,N,M.(1 <= M <= N <= 200);在接下来的N行里。每行包含2个整数。a,b. 在第 i 行,a 代表要攻克第 i 个城堡必须先攻克第 a 个城堡,假设 a = 0 则代表能够直接攻克第 i 个城堡。

b 代表第 i 个城堡的宝物数量, b >= 0。当N = 0, M = 0输入结束。

 
Output
对于每一个測试实例。输出一个整数。代表ACboy攻克M个城堡所获得的最多宝物的数量。
 
Sample Input
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
 
Sample Output
5
13
dp[i][j]表示以i为根节点j个子节点的最大值。
#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:
 1000MS
  Memory 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 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<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的更多相关文章

  1. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. 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 ...

  4. hdu 1520 Anniversary party 基础树dp

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  5. 【BZOJ】1040: [ZJOI2008]骑士(环套树dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1040 简直不能再神的题orz. 蒟蒻即使蒟蒻,完全不会. 一开始看到数据n<=1000000就 ...

  6. [51NOD1405] 树的距离之和(树DP)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...

  7. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  8. [HDOJ2196]Computer (树直径, 树DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 给一棵树,求树上各点到某点的距离中最长的距离.注意每个点都要求. 和普通求树的直径不一样,要求每 ...

  9. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  10. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

随机推荐

  1. 新的方法 (New Approach)¶

    第一章:简介 - ANSI Common Lisp 中文版 新的方法 (New Approach)¶ 本书的目标之一是不仅是教授 Lisp 语言,而是教授一种新的编程方法,这种方法因为有了 Lisp ...

  2. 通过加载Kernel32来动态判断 当前操作系统32bit还是64bit

    工作原理:通过加载Kernel32来获取IsWow64Process 函数然后通过函数的地址操作,执行函数的操作. 在程序中只要我们获取了一个函数的地址,就可以找到正确的方法执行这个函数. 但是这种方 ...

  3. asp.net(C#)之NPOI&quot;操作Excel

    1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...

  4. 腾讯測试project师笔试面试记录

        从3月29日參加腾讯笔试開始,開始了为期1周的腾讯之旅,尽管最后还是跪在了二面上,可是感觉收获非常多,至少明确了自己与向往的BAT公司的差距,明确了自己还是路漫漫其修远兮.     腾讯非常注 ...

  5. ORACLE:plsql优化

      1.登录后默认自动选中My Objects? 设置方法:Tools菜单--Brower Filters会打开Brower Filters的定单窗口,把“My Objects”设为默认即可.? 同理 ...

  6. LINUX专题之操作系统字符集

    原创作品,出自 "深蓝的blog" 博客,欢迎转载.转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog: http://blog.csdn.net/huangyanl ...

  7. Libevent使用样例,从简单到复杂

            转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂.展示怎样使用libevent.网上的很多 ...

  8. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  9. 32位Linux文件限制大小

    线上程序不断重新启动,查看log发现是进程由于SIGXFSZ信号退出.对过大的文件进行操作的时候会产生此信号,一般仅仅在32位机器上出现,文件限制大小为2G.用lsof查看进程打开的文件,果然有一个文 ...

  10. Photon + Unity3D 在线游戏开发 学习笔记(两)

    本文和大家 和大家说说 Photon 解压后的目录结构 这里面最基本的我们 以后开发要用到的目录 就是  deploy目录,这个目录里 放的是要挂载的 server 当然我们的 server端也要放在 ...