HDU 1520 Anniversary Party (DFS or tree DP)

Source: Internet
Author: User

Problem Descriptionthere is going to be a party to celebrate the 80-th anniversary of the Ural state University. The University has a hierarchical structure of employees. It means the supervisor relation forms a tree rooted at the Rector v. E. Tretyakov. In order to make the party funny for every one, the rector does not want both a employee and his or her immediate supervi Sor to is present. The personnel office has evaluated conviviality of all employee, so everyone had some number (rating) attached to him or Her. Your task is to make a list of guests with the maximal possible sum of guests ' conviviality ratings.

Inputemployees is numbered from 1 to N. A first line of input contains a number n. 1 <= n <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from-128 to 127. After the go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the k-th employee was an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Outputoutput should contain the maximal sum of guests ' ratings.

Sample Input
711111111 32 36 47 44 53 50 0

Sample Output
5
Test instructions: N people go to the party, which has a direct relationship between the parties will affect the atmosphere, so you can not participate in the party will have an active, and now you calculate from the n person selected some people, so that the event is the most active
idea: Very simple, everyone participates and does not participate will have different results, he participates, then only need to add his subordinates not to participate in the weight, he does not participate, then plus his subordinates to participate in or not to participate in the maximum value, finally only need to calculate the highest boss or not to participate in the maximum value can be
PS: The code of the tree DP you go to the other person's bar, recently training in the Blue Bridge Cup, so the enhanced DFS capabilities
AC Code:
#include <cstdio> #define N 6005struct p{int FM;    int child;    int brother;    int att;    int no;        void Init () {no=0;        fm=0;        brother=0;    Child=0;    } int Max () {return att>no? att:no;    }}num[n];void dfs (int x) {int child=num[x].child;        while (child) {DFS (child);        num[x].att+=num[child].no; Num[x].no+=num[child].        Max ();    Child=num[child].brother;    }}int Main () {int n;            while (~SCANF ("%d", &n)) {for (int i=1;i<=n;i++) {num[i].init ();        scanf ("%d", &num[i].att);        } int A, b; while (scanf ("%d%d", &a,&b), a| |            b) {num[a].fm=b;            Num[a].brother=num[b].child;        Num[b].child=a;                } for (int i=1;i<=n;i++) {if (num[i].fm==0) {DFS (i); printf ("%d\n", Num[i].                Max ());            Break }}} RETUrn 0;} 


HDU 1520 Anniversary Party (DFS or tree DP)

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.