1380 Prom without a boss
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingTitle 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
Category labels
Tags Click here to expand
Thinking Analysis:
Topic simplified version (same idea)
Example code
1#include <cstdio>2#include <iostream>3#include <vector>4 #defineN 550005 using namespacestd;6 intf[n][2],fa[n],n;7vector<int>V[n];8 voiddpintx)9 {Ten for(intj=0; J<v[x].size (); j + +){ One intI=V[x][j]; A DP (i); -f[x][0]+=max (f[i][0],f[i][1]); -f[x][1]+=f[i][0]; the } -f[x][1]++; - } - intMain () + { -scanf"%d",&n); + for(intI=1, x,y;i<n;i++){ Ascanf"%d%d",&x,&y); atfa[x]=y; - v[y].push_back (x); - } - for(intI=1; i<=n;i++) - if(fa[i]==0){ - DP (i); inprintf"%d\n", Max (f[i][0],f[i][1])); - return 0; to } +}
is the tree-type DP Board
1380 Prom code without a boss
1#include <cstdio>2#include <iostream>3#include <vector>4 #defineN 550005 using namespacestd;6 intf[n][2],fa[n],a[n],n,x,y;7vector<int>V[n];8 voiddpintx)9 {Ten for(intj=0; J<v[x].size (); j + +) {//how many edges of the loop are connected to x One intI=V[X][J];//One of the sons of X ADP (i);//recursion to the bottom -f[x][0]+=max (f[i][0],f[i][1]);//if x is not selected, the lower level can be selected or not selected -f[x][1]+=f[i][0];//if X is selected, the lower layer must not be selected the } -f[x][1]+=A[X];//if X is selected, add itself - } - intMain () + { -scanf"%d",&n); + for(intI=1; i<=n;i++) Ascanf"%d", A +i); at while(SCANF ("%d%d", &x,&y) = =2&&x&&y) { -Fa[x]=y;//X's father is Y. -V[y].push_back (x);//V[y (father number)][0-number of sides (table)]=x (son number); - } - for(intI=1; i<=n;i++) - if(fa[i]==0){//Find the root in DP (i); -printf"%d\n", Max (f[i][0],f[i][1]));//Root may have more than one son, choose not to choose the best root to return 0; + } -}
1380 Prom without a boss