POJ 2342 Anniversary party 树形DP基础题

题目链接:http://poj.org/problem?id=2342

题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人就不可以邀请其的直属员工,
问如何使得这个快乐值达到最大。

题解:对每个结点dp[i][0]表示不邀请这个员工,其子树达到的最大快乐值,dp[i][1]表示邀请i员工其子树达到的最大值。
dp[i][0]=(i的全部员工的max(dp[u][1],dp[u][0)相加,也就是其子员工来或不来的最大快乐值。
dp[i][1]=(i的全部员工的dp[u][0相加,也就是其子员工都不能不来的最大快乐值。
从大boss开始dp,最终结果就是max(dp[root][0],dp[root][1])

代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 6060;

int n, happy[maxn], dp[maxn][2], p[maxn];
vector<int> sons[maxn];
bool vis[maxn];

void dfs(int u) {
    if (vis[u]) return;
    vis[u] = true;
    dp[u][1] = happy[u], dp[u][0] = 0;
    for (int i = 0; i < sons[u].size(); i ++) {
        int v = sons[u][i];
        if (v == p[u]) continue;
        dfs(v);
        dp[u][1] += dp[v][0];
        dp[u][0] += max(dp[v][1], dp[v][0]);
    }
}

void test() {
    for (int i = 1; i <= n; i ++) {
        cout << "dp[" << i << "] = " << dp[i][1] << " , " << dp[i][0] << endl;
    }
}

int main() {
    while (cin >> n) {
        if (!n) break;
        fill(p+1, p+1+n, -1);
        fill(vis+1, vis+1+n, false);
        for (int i = 1; i <= n; i ++) cin >> happy[i];
        int a, b;
        for (int i = 1; i < n; i ++) {
            cin >> a >> b;
            p[a] = b;
            sons[b].push_back(a);
        }
        int root = 1;
        while (p[root] != -1) root = p[root];
        dfs(root);
        cout << max(dp[root][1] , dp[root][0]) << endl;
        // test();
    }
    return 0;
}

参考链接:http://www.cnblogs.com/seaupnice/p/9471700.html

原文地址:https://www.cnblogs.com/zifeiy/p/10711903.html

时间: 2024-08-05 14:39:48

POJ 2342 Anniversary party 树形DP基础题的相关文章

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

POJ 2342 Anniversary party 树形dp入门

http://poj.org/problem?id=2342 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大. 对于每一个人,都有两种情况,选,或者不选.然后选了后,他的下属就只能不选了,如果它不选,下属可能选或者不选. 这样用dfs + 记忆化搜索,就好了 每次搜索,就是要求以第cur个节点为根的子树中,其中第cur个节点的状态是s

POJ 2342 Anniversary Party ( 树形DP )

deque 的插入操作不一定有 vector 快 #include <iostream> #include <cstring> #include <fstream> #include <vector> using namespace std; #define NOT_SELECTED 0 #define SELECTED 1 #define SIZE 6001 vector< int > relations[SIZE]; bool visited

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][

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

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

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

POJ 2342 Anniversary party (树dp)

题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的活跃值是多少. 也就是说一棵树中,选择的点不能是相邻的点,且要使活跃值最大. 简单的树形dp,任意选一个点开始遍历,从叶子节点开始回溯. dp[i][0]表示不选i节点最大的活跃度,则dp[i][1]表示选i节点最大的活跃度. i与j相连,dp[i][0] += max(dp[j][0], dp[j

POJ 1947 Rebuilding Roads (树形dp 经典题)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9499   Accepted: 4317 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

HDU1520 Anniversary party 树形DP基础

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