首页 > 学院 > 开发设计 > 正文

POJ 2342 Anniversary party (最基础树形DP入门)

2019-11-10 19:07:47
字体:
来源:转载
供稿:网友

Anniversary party
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7574 Accepted: 4340

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

711111111 32 36 47 44 53 50 0

Sample Output

5

题意:

某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。

树形DP:dp[i]// 以i号人为根的关系树,dp [i][1]表示当前树 i 号人出席的价值和最大值,表示当前树 i 号人不出席的价值和最大值。

方程:dp[i][0] +=Σ max(dp[son][0],dp[son][1]);

   dp[i][1] +=Σ dp[son][0];

dp[son] 在dp[i]之前算,所以DFS。

没什么好说的。。还是很水的,通过这个了解下树形DP的概念吧,树形DP通常有“树”的关系,一般通过叶子节点向根节点传递信息,所以一般dfs在转移方程之前。。还有就是怎么找根节点,要明白如果是一颗树的话,只有根节点没有父节点,其余的都有一个,所以找到那个没有父节点的节点就是根节点,另外明白树的构造,每两个点之间都由一条边,两个子树之间没有边直接把他们连接。

vector存图

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define me(a) memset(a, 0, sizeof(a))using namespace std;const int maxn = 6e3+5;//vector <int> v[maxn];vector< vector<int > > v(maxn);int dp[maxn][2], book[maxn], n;void dfs(int x){    for(int i = 0; i < v[x].size(); i++)    {        int to = v[x][i];        dfs(to);        dp[x][1] += dp[to][0];        dp[x][0] += max(dp[to][1], dp[to][0]);    }}int main(){    scanf("%d", &n);    for(int i = 1; i <= n; i++)        scanf("%d", &dp[i][1]);    int x, y;    for(int i = 1; i < n; i++)    {        scanf("%d%d", &x, &y);        v[y].push_back(x);        book[x] = 1;   //这里是找根节点    }    scanf("%d%d", &x, &y);    int rt;    for(int i = 1; i <= n; i++)    {        if(!book[i])        {            rt = i;            break;        }    }    dfs(rt);    printf("%d/n", max(dp[rt][0],dp[rt][1]));    return 0;}网上前向星存图代码:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;long long dp[6666][2];int cnt;int head[6666];int v[6666];int in[6666];struct edge{    int to,next;}E[6666];void addedge(int from , int to){    E[cnt].to = to;    E[cnt].next = head[from];    head[from] = cnt++;}void dfs(int now){     for (int i = head[now] ; i != -1 ; i = E[i].next )     {        int to = E[i].to;        dfs(to);        dp[now][0] += max(dp[to][1],dp[to][0]);        dp[now][1] += dp[to][0];     }     return;}int main(){    int N;    //freopen("in.txt","r",stdin);    ios::sync_with_stdio(false);    cin >> N;    memset(dp,0,sizeof(dp));    for (int i = 1; i <= N ; i++)    {        cin >> dp[i][1];    }    int a,b;    cnt = 0;    memset(head,-1,sizeof head);    long long start = N*(N+1)/2;    while (cin >> a >> b &&a!=0&&b!=0)    {        addedge(b,a);        start -= (long long)a;    }    dfs((int)start);    cout << max(dp[start][1],dp[start][0]) << endl;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表