Rocky Valley 1352 no boss's prom.

Source: Internet
Author: User

Rocky Valley 1352 no boss's prom.

Address: http://www.luogu.org/problem/show?pid=1352

Title Description

A university has N staff, numbered 1~n. They have a subordinate relationship, which means that their relationship is like a tree rooted in the headmaster, and the parent node is the direct boss of the child node. There's an anniversary party, and every invitation to an employee adds a certain amount of happiness to the RI, but if an employee's boss comes to the ball, the clerk will not come to the party anyway. So, please do your programming, invite the staff can make the happiness index biggest, seek the biggest happiness index.

Input/output format

Input format:

The first line is an integer n. (1<=n<=6000)
Next n lines, line i+1 represents the happiness index RI for staff i. ( -128<=ri<=127)
Next N-1 line, enter a pair of integer l, K for each line. Indicates that K is the direct boss of L.
Last line input 0 0

Output format:

Output the maximum happiness index.

Input/Output sample

Input Sample # #:

7

1

1

1

1

1

1

1

1 3

2 3

6 4

7 4

4 5

3 5

0 0

Sample # # of output:

5

Ideas

DP on Classic tree.

Set D[I][J] Indicates the maximum happiness value when the subtree state is s with the root of I, and s==0 indicates that there is no s==1 present. Then there is the transfer equation:

d[i][1]=sum{d[chi][0]}

D[I][0]=SUM{MAX{D[CHI][0],D[CHI][1]}}

It is important to note that when s==0, the subtree status is not necessarily 1.

Code

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <vector>5 using namespacestd;6 7 Const intMAXN =6000+Ten;8 intr[maxn],d[maxn][2],CNT[MAXN];9vector<int>G[MAXN];Ten intN; One  A voiddpintu) { -     if(d[u][0])return ; -      the      for(intI=0; I<g[u].size (); i++) { -         intv=G[u][i]; - DP (v); -d[u][1] + = d[v][0]; +d[u][0] + = max (d[v][0],d[v][1]);//Max -     } +d[u][1] +=R[u]; A } at  - intMain () { -scanf"%d",&n); -      for(intI=1; i<=n;i++) scanf ("%d",&r[i]); -     intu,v; -      while(SCANF ("%d%d", &u,&v) && u &&v) inG[v].push_back (u), cnt[u]++; -     intRoot; to      for(intI=1; i<=n;i++)if(!Cnt[i]) { +root=i; Break; -     } the DP (root); *printf"%d", Max (d[root][1],d[root][0])); $     return 0;Panax Notoginseng}

Rocky Valley 1352 no boss's prom.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.