HDU 1520-Anniversary party(树形dp入门)

题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值。

分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1,i参加 能达到的最大欢乐值。i参加i的孩子不能参加,i不参加,其孩子参不惨加都行(取最大值)。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
struct tree{
    int u,v,next;
} t[6010];
int par[6010],dp[6010][2],n,head[6010],len,root;
void add(int a,int b){
    t[len].u=a;
    t[len].v=b;
    t[len].next=head[a];
    head[a]=len++;
}
void dfs(int root){
    for(int i=head[root];i!=-1;i=t[i].next){
        int son=t[i].v;
        dfs(son);
        dp[root][0]+=max(dp[son][0],dp[son][1]);
        dp[root][1]+=dp[son][0];
    }
}
int main()
{
    while(~scanf("%d",&n)){
        memset(par,0,sizeof(par));
        memset(dp,0,sizeof(dp));
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;++i)
            scanf("%d",&dp[i][1]);
            int c,f;
            len=0;
            while(scanf("%d%d",&c,&f)){
                if(c==0&&f==0)break;
               add(f,c);
               par[c]=1;
            }
            for(int i=1;i<=n;++i){
                if(!par[i]){
                    root=i;
                    break;
                }
            }
            dfs(root);
            printf("%d\n",max(dp[root][0],dp[root][1]));
    }
return 0;
}
时间: 2024-12-29 06:54:03

HDU 1520-Anniversary party(树形dp入门)的相关文章

POJ 2342 &amp;&amp;HDU 1520 Anniversary party 树形DP 水题

一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点)都不能被邀请 2.每一个员工都有一个兴奋值,在满足1的条件下,要使得邀请来的员工的兴奋值最高 输出最高的兴奋值. 简单的树形DP dp[i][1]:表示以i为根的子树,邀请节点i的最大兴奋值 dp[i][0]:表示以i为根的子树,不邀请节点i的最大兴奋值 先根据入度找出整棵树的根节点, 然后一次DF

HDU 1520 Anniversary party (树形DP)

树形DP的关键在于如何处理递归返回的信息.这题dp[i][0]表示不选i点时当前最高权值.dp[i][1]表示选i点时当前最高权值.状态转移方程:dp[u][[0]+=max(dp[v][0],dp[v][1]),dp[u][1]+=dp[v][0]; 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm&

hdu oj 1520 Anniversary party(树形dp入门)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6990    Accepted Submission(s): 3104 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

[poj2342]Anniversary party树形dp入门

题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max(dp[j][0],dp[j][1]); 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<iostream> 6

Anniversary party(树形DP入门)

Anniversary party HDU - 1520 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 rect

HDU - 1520 Anniversary party (有向入门树形DP)

题目链接:https://vjudge.net/problem/HDU-1520 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

POJ 2342 Anniversary party (树形dp 入门题)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4810   Accepted: 2724 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

hdu 1011 Starship Troopers(树形DP入门)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17498    Accepted Submission(s): 4644 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of t