HDU 1520 Anniversary Party tree DP First question

Source: Internet
Author: User

Anniversary party

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 8667 Accepted Submission (s): 3748

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 Input711111111 32 36 47 44 53 50 0

Sample OUTPUT5

Sourceural State University Internal Contest October ' Students SessionThe main idea is that there is a group of people have a relationship between the subordinate, in a party, there is a direct relationship between the subordinate (that is, the parent-child relationship in the tree) can not attend at the same time, everyone has a rating, smell how to choose to attend the people, so that all the rating and the biggest
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf =0x7f7f7f7f;const double pi=acos ( -1); const int Maxn=40000;int n,x,y,a[    6005],dp[6005][3],indeg[6005];vector<int> G[6005];int Solve () {int ans=0;    Queue<int> Q;          for (int i=1;i<=n;i++) {Ans=max (ans,a[i]);          Dp[i][1]=a[i];          dp[i][0]=0;      if (!indeg[i]) {Q.push (i);}      } while (Q.size ()) {int U=q.front (); Q.pop ();          for (int i=0;i<g[u].size (); i++) {int v=g[u][i];          Dp[v][0]+=max (dp[u][0],dp[u][1]);          DP[V][1]+=DP[U][0];          indeg[v]--;          if (!indeg[v]) Q.push (v); Ans=max (ans, Max (dp[v][0],dp[v][1])); }} return ans;        int main () {while (~SCANF ("%d", &n)) {for (int i=1;i<=n;i++) {g[i].clear (); scanf ("%d", &a[i]);}        MM (indeg,0);        for (;;)            {scanf ("%d%d", &x,&y);            if (!x&&!y) break;            G[x].push_back (y);        indeg[y]++;    } printf ("%d\n", Solve ()); } return 0;}

Analysis: Should be the most basic tree-shaped DP bar, according to the DAG after the map, from the leaf node U consider (it can only be a child node), set

DP[U][0] is the maximum rating that can be obtained when the U node is not selected

DP[U][1] The maximum rating that can be obtained by selecting the U-node

To construct the state transition equation for its parent node V:

Dp[v][0]+=max (dp[u][0],dp[u][1]);(can only choose one of them)

DP[V][1]+=DP[U][0];

Finally, just follow the DAG and sweep up.

HDU 1520 Anniversary Party tree DP First question

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.