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

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;
}
时间: 2024-10-10 14:12:07

DP:树DP的相关文章

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

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1),我们可以简单地把0当作根,这样下来父子关系就确定了. (2)定义数组num[x]表示以节点x为根的子树有多少个节点,dp[x]是我们所求的--所有节点到节点x的距离之和. (3)在步骤(1)中,其实我们同时可以计算出 num[x],还可以计算出每个节点的深度(每个到根节点0的距离),累加全部节点

第十四个目标(dp + 树状数组)

Problem 2236 第十四个目标 Accept: 17    Submit: 35 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 目暮警官.妃英里.阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶.在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关. 为了避免

2010辽宁省赛 NBUT 1222 English Game【字典树+DP】

[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 链接:Click Here! 问题描述 This English game is a simple English words connection game. The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a wei

uva 12452 Plants vs. Zombies HD SP (树DP)

Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a grid, but on a connected graph G with no cycles (i.e., a tree). Zombies live on edges of the tree and chew through edges so that tree falls apart! Pl

hdu 3016 Man Down (线段树 + dp)

题目大意: 是男人就下一般层...没什么可以多说的吧. 注意只能垂直下落. 思路分析: 后面求最大值的过程很容易想到是一个dp的过程 . 因为每一个plane 都只能从左边 从右边下两种状态. 然后我们所需要处理的问题就是 ,你如何能快速知道往左边下到哪里,往右边下到哪里. 这就是线段树的预处理. 讲线段按照高度排序. 然后按照高度从小到大加入到树中. 然后去寻找左端点 和 右端点最近覆盖的线段的编号. #include <cstdio> #include <iostream> #

ZOJ3632 线段树+DP

买西瓜吃,每个西瓜有两个参数,一个是p代表价格,一个是t代表能吃几天,要求n天每天都能吃西瓜,而且如果你今天买了,以前买的还没吃完 那么都得扔了,求最小花费,还真想不到用线段树+DP,最后看了一下别人的标题,想了一下,DP方程挺好推的,线段树也只是单点查询, #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #inclu

hdu-2169 Computer(树形dp+树的直径)

题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new co

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

题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is

hdu 4863 Centroid of a Tree 树dp

代码来自baka.. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<stack> #include<set> #include<cmath> #include<vecto

[noi2013]快餐店 基环树dp,单调队列维护最大值和次大值

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 220000 #define inf 0x3ffffffffffffffLL typedef long long ll; int v[N],e[N],ne[N],nn,w[N]; void add(int x,int y,int z){ ne[++nn