Prom without a boss (HDU 1520)

Source: Internet
Author: User

Title Description Description

Ural University has a staff of N and is numbered 1~n. They have affiliation, 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. Each employee has a happiness index. There is now an anniversary party, which asks the staff to have the most happiness index. However, no staff is willing to attend with the direct boss.

Enter a description Input Description

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 description Output Description

Output the maximum happiness index.

Sample input 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 Sample Output

5

Data range and Tips Data Size & Hint

Each test point 1s

/*tree-shaped DP*/#include<cstdio>#include<iostream>#include<cstring>#include<vector>#defineM 6010using namespacestd;intVis[m],inch[m],f[m][2],n;vector<int>G[m];voidDfsintx) {    if(Vis[x])return; VIS[X]=1;  for(intI=0; I<g[x].size (); i++)    {        inty=G[x][i];        DFS (y); f[x][0]+=max (f[y][0],f[y][1]); f[x][1]+=f[y][0]; }}voidinit () { for(intI=1; i<=n;i++) scanf ("%d", &f[i][1]);  while(1)    {        intx, y; scanf ("%d%d",&x,&y); if(x==0&&y==0) Break;        G[y].push_back (x); inch[x]++; }    intp=0;  for(intI=1; i<=n;i++)      if(!inch[i]) {p=i;          DFS (i);  Break; } printf ("%d\n", Max (f[p][0],f[p][1]));  for(intI=1; i<=n;i++) G[i].clear ();}intMain () { while(SCANF ("%d", &n) = =1) {memset (inch,0,sizeof(inch)); Memset (F,0,sizeof(f)); memset (Vis,0,sizeof(VIS));    Init (); }}
View Code

Prom without a boss (HDU 1520)

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.