hdu-1520 Anniversary Party (tree-shaped DP)

Source: Internet
Author: User

Topic Links:

Anniversary party

Time limit:2000/1000 MS (java/others)

Memory limit:65536/32768 K (java/others)


problem DescriptionThere is going-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 Input7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample OUTPUT5 Test Instructions:
to each point of the weight, and some of the points of the parent-child relationship, the parent node and child nodes can not appear at the same time, ask sum maximum is how much; Ideas:Tree-shaped DP template problem, feel the first write tree DP is similar to DP, but with the tree has a relationship, and the title of the weight may be negative, I feel may be this WA, did not think of AC; AC Code:
/*1520 124MS 1988K 1430 B g++ 2014300227*/#include<bits/stdc++.h>using namespacestd;Const intn=6e3+4; typedefLong Longll;Const DoublePi=acos (-1.0);intn,a[n],vis[n],dp[n][2],fa[n];vector<int>Ve[n];voidDfsintx) {Vis[x]=1; intlen=ve[x].size (); if(len==0) {dp[x][0]=0; dp[x][1]=max (0, a[x]); return ; }    Else {    intsum1=0, sum2=0;  for(intI=0; i<len;i++)    {        inty=Ve[x][i]; if(!Vis[y]) dfs (y);//Add a judgment to avoid repeating the iteration; Sum1+=max (dp[y][0],dp[y][1]); Sum2+=dp[y][0]; } dp[x][0]=sum1; dp[x][1]=sum2+max (0, a[x]); }}intMain () { while(SCANF ("%d", &n)! =EOF) {         for(intI=1; i<=n;i++) {Fa[i]=-1; Vis[i]=0; dp[i][0]=dp[i][1]=0;            Ve[i].clear (); scanf ("%d",&A[i]); }        intu,v;  while(1) {scanf ("%d%d",&u,&v); if(u==0&&v==0) Break; Fa[u]=v;        Ve[v].push_back (U); }        intans=0;  for(intI=1; i<=n;i++)        {            if(!Vis[i])            {DFS (i); }        }         for(intI=1; i<=n;i++)        {            if(fa[i]==-1)            {                intMmax=max (dp[i][1],dp[i][0]); if(mmax>0) ans+=Mmax; }} printf ("%d\n", ans); }    return 0;}

hdu-1520 Anniversary Party (tree-shaped 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.