【HDU】1520-Anniversary party(初级树形DP)

不是什么难题,但也算自己写的第一个树形DP,留个纪念吧= =

状态方程(dp[0][i]代表不选i人参加聚会时候的最大值,dp[1][i]代表选)

dp[0][pos] = max(dp[0][pos],max(dp[0][e] + dp[0][pos],dp[1][e] + dp[0][pos]));
dp[1][pos] = max(dp[1][pos],dp[1][pos] + dp[0][e]);
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 6005;
int n;
int v[maxn];
int vis[maxn];
int dp[2][maxn];
vector<int>G[maxn];
void dfs(int pos){
    if(!G[pos].size()){
        dp[0][pos] = 0;
        dp[1][pos] = v[pos];
        return;
    }
    dp[0][pos] = 0;
    dp[1][pos] = v[pos];
    for(int i = 0; i < G[pos].size(); i++){
        int e = G[pos][i];
        dfs(e);
        dp[0][pos] = max(dp[0][pos],max(dp[0][e] + dp[0][pos],dp[1][e] + dp[0][pos]));
        dp[1][pos] = max(dp[1][pos],dp[1][pos] + dp[0][e]);
    }
    return;
}
int main(){
    while(scanf("%d",&n) != EOF){
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n; i++) G[i].clear();
        for(int i = 1; i <= n; i++)
            scanf("%d",&v[i]);
        int a,b;
        while(scanf("%d%d",&a,&b)){
            if(!a && !b) break;
            G[b].push_back(a);
            vis[a] = 1;
        }
        int s;
        for(int i = 1; i <= n; i++)
            if(!vis[i]){
                s = i;
                break;
            }
        dfs(s);
        printf("%d\n",max(dp[0][s],dp[1][s]));
    }
    return 0;
}
时间: 2024-11-05 14:40:53

【HDU】1520-Anniversary party(初级树形DP)的相关文章

HDU 1520 Anniversary party(树形DP)

Anniversary party Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 81   Accepted Submission(s) : 31 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description There is going to b

HDU 1520 Anniversary party (树形DP,入门)

题意:给一棵树,每个节点都有权值,要求选择部分节点出来,使得权值之和最大,但是每对(父亲,儿子)中最多只能挑一个. 思路: 比较入门的题,每个节点可以选也可以不选.若当前节点选的话,孩子必须全部不选:若当前节点不选,则孩子可以选也可以不选. 1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 #define INF 0x3f3f3f3f 4 #define LL long long 5 using namespace s

【HDU】1520 Anniversary party(树形dp)

题目 题目 分析 带权值的树上最大独立集 代码 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=6005; 4 int a[maxn], n, fa[maxn]; 5 vector<int> son[maxn]; 6 7 int dp(int u) 8 { 9 int sum1=a[u],sum2=0; 10 if(son[u].empty()) 11 { 12 return a[u]; 13 }

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 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU 1520 Anniversary party(树形DP-最大独立集)

大意:很多领导,能形成一个树形关系网,这些领导参加一个party,每个人都有一个能使party活跃的值,但是每个人又不喜欢跟自己的直接领导同时参加party.为使party气氛最好,求最好气氛值. 思路: 法一:对子树的根按两种决策找到状态方程,然后用刷表法 法二:细化状态,dp[i][0],dp[i][1] 分别表示不选i时的最大集和选了i时的最大集 法二的方法更实用,状态细化后更便于找状态方程 法二代码: #include<cstdio> #include<cstring> #

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>

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

poj2342 Anniversary party (树形dp)

poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   Accepted: 5250 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarc

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 4118 Holiday&#39;s Accommodation 树形dp

Holiday's Accommodation Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4118 Description Nowadays, people have many ways to save money on accommodation when they are on vacation.One of these ways is exchanging