2019 summer training Anniversary Gala


Title Description
Chancellor Ural State University is preparing to commemorate the school's 80th anniversary party. Because of the school's staff have a different job level, it may constitute a principal with human relations as the root of the tree. Each resource has a unique integer number, numbered from 1 to N, and corresponds to the joy of participating in a gathering obtained. To make each employee feel happy, principals try to make each employee and their direct supervisor is not the party at the same time.

Your task is to design a party's list of participants, the highest total joy degrees.
Entry
The first row is an integer N;

Then joyful N rows corresponding to the N of the staff, the i-th row is the joy an integer of i-th pi staff;

followed by school personnel relation tree, each row format LK, K denotes a clerk is the direct supervisor of the L staff, to 00 input end.
Export
Output joy of participating in the largest gathering of those available.
Sample input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Sample Output
5


As the network of relationships was a tree, it is easy to think of the tree dp 
So for each person we have to (1) do not (0) are two possibilities:
(1) the first individuals to x, then his son can not go to that dp [ x] [. 1] + DP = [Y] [0];
(2) the x personal not, it may not son may go two possible minimum value, the dp [x] [0] + = min (dp [y] [0 ], dp [y] [1]);
therefore the Code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,p[10050],dp[10050][2],head[10050],num,book[10050],root=1;
struct edge
{
    int u,v,nxt;
}e[200500];
void add(int u,int v)
{
    e[++num].u=u;e[num].v=v;
    e[num].nxt=head[u],head[u]=num;
}
void DP(int x,int fa)
{
    for(int st=head[x];st!=-1;st=e[st].nxt)
    {
        int y=e[st].v;
        if(y==fa)continue;
        DP(y,x);
//      dp[x][0]=max(dp[x][0],dp[y][1]);
//      dp[x][1]=max(dp[x][1],dp[y][0]+p[x]);
        dp[x][0]+=max(dp[y][1],dp[y][0]);
        dp[x][1]+=dp[y][0];
    }
    dp[x][1]+=p[x];
    return;
}
int main()
{
    memset(head,-1,sizeof head);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&p[i]);
    int l=1,k=1;
    while(!(l==0&&k==0))scanf("%d%d",&l,&k),add(k,l),book[l]=1;
    while(book[root])root++;
    DP(root,-1);
//  putchar('\n');
//  putchar('\n');
//  for(int i=1;i<=n;i++)printf("%d %d\n",dp[i][0],dp[i][1]);
    int ans=max(dp[root][0],dp[root][1]);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qxds/p/11344347.html