Luo Valley P1352 no boss party (DP + tree of memory)

Title Description

There are a university nn the n-staffs, numbered 1 ... N1 \ ldots the n- 1 ... the n-.

There are dependencies between them, that is their direct supervisor relationship is like a tree rooted to the principal, the parent node is the child node.

Now there is the anniversary banquet, invited to a banquet every employee will increase a certain happiness index rir_i r i , but then, if the direct supervisor of a staff member to attend the prom, then the staff would in any case have refused to participate dance party.

Therefore, you programmed computing, which allows staff to invite the greatest happiness index, find the greatest happiness index.

Input Format

The first line of the input is an integer NN n-.

Of 22 is 2 to (n-+. 1) (n-+. 1) ( n- + . 1 ) lines, each an integer, the (I +. 1) (I +. 1) ( I + . 1 an integer) line represents II I No. employee happiness index rir_i r i .

Of (n-+ 2) (n-+ 2) ( n- + 2 ) to the second (2N +. 1) (2N +. 1) ( 2 n- + . 1 ) lines, each pair of input integer L, kl to, K L , K representative KK K is ll direct supervisor of l.

Output Format

An integer output line represents the greatest happiness index.

Sample input and output

Input # 1
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Output # 1
5 
tree DP classic example. For some roots for no more than two cases: the election and not vote root roots. In dp [i] [0] i represents the selected person, dp [i] [1] indicates the person is not selected from i. If selected, then dp [i] [0] to add their own happiness index tired, then sweep this under the subordinate person recursive (Note memory of), says that the title is not a direct supervisor if possible. dp [i] [1] on the accumulation result for each i obtained under the person's recursion, and finally returns the larger that value.
#include <bits / STDC ++ H.>
 #define N 6005 
 the using  namespace STD;
 int A [N], head [N], Ver [N], the Next [N], TOT = 0 , n-;
 int DP [N] [ 2 ] = { 0 }; // DP [i] [0] represents a group selected under i instead of i is selected from dp [i] [1] represents a group selected subordinate not selected from i i 
BOOL VIS [ 6005 ] = { 0 } ;
 void the Add ( int X, int Y) 
{ 
    Ver [ ++ TOT] = Y; 
    the Next [TOT] = head [X]; 
    head [X] = TOT; 
} 
int Process ( int r)
{
    if(dp[r][0]||dp[r][1])
    {
        return max(dp[r][0],dp[r][1]);
    }
    int i,j;
    dp[r][0]+=a[r];
    for(i=head[r];i;i=Next[i])
    {
        int y=ver[i];
        dp[r][1]+=process(y);
    }
    for(i=head[r];i;i=Next[i])
    {
        int y=ver[i];
        for(j=head[y];j;j=Next[j])
        {
            int z=ver[j];
            dp[r][0]+=process(z);
        }
    }
    return max(dp[r][0],dp[r][1]); 
}
int main()
{
    int i;
    cin>>n;
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(x&&y)
        {
            add(y,x);
            vis[x]=1;
        }
    }
    int root;
    for(i=1;i<=n;i++)
    {
        if(!vis[i])//寻找树根 
        {
            root=i;
            break;
        }
    }
    cout<<process(root); 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12541297.html