【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     }
14     for(int i=0;i<son[u].size();i++)
15     {
16         for(int j=0;j<son[son[u][i]].size();j++)
17         sum1+=dp(son[son[u][i]][j]);
18         sum2+=dp(son[u][i]);
19     }
20     int ans=max(sum1,sum2);
21     return ans;
22 }
23 int main()
24 {
25     int k,l;
26     while(scanf("%d",&n)==1)
27     {
28         for(int i=1; i<=n;i++) scanf("%d",&a[i]);
29         while(scanf("%d%d",&k,&l) && k)
30         {
31             son[l].push_back(k);
32             fa[k]=1;
33         }
34         int Be=1;
35         for(int i=1;i<=n;i++)
36             if(!fa[i]) { Be=i; break; }
37
38         printf("%d\n",dp(Be));
39
40         for(int i=1;i<=n+1;i++) son[i].clear();
41         memset(fa,0,sizeof(fa));
42     }
43
44     return 0;
45 }

原文地址:https://www.cnblogs.com/noblex/p/8401489.html

时间: 2024-10-06 00:38:56

【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 1520 Anniversary party 树DP水题

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

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 1520Anniversary party(简单树形dp)

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

HDU 1011 Starship Troopers(树形DP)

Starship Troopers Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description You, the leader of

poj 2324 Anniversary party(树形DP)

/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ---于是当i去时,i的所有儿子都不能去:dp[i][1]=sum(dp[j][0])+a[i],其中j是i的儿子节点. ---当i不去时,i的儿子可去也可不去:dp[i][0]=sum(max(dp[j][0],dp[j][1])),j是i的儿子节点 ---边界条件:当i时叶子节点时,dp[i][

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

HDU 1520.Anniversary party 基础的树形dp

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