Vijos 1642 The task of the monitor tree DP

Source: Internet
Author: User

the task of P1642 Squad leaderBackground

18 The commencement ceremony of the lay (1)

Describe

Fuzhou Times Middle School 2009 session 10 classmates graduated, so monitor PRT began to prepare graduation party, but due to limited conditions, may each classmate can not go, but everyone has a right value, PRT hope to come to the students of the total weight.

Class 10 has a careful telephone notification network, it is actually a tree, root node for monitor PRT, by her to inform her downline (that is, son node), the downline continue to inform their downline (not necessarily every downline to notify), anyone can not go: "
In order to make the largest sum of weights, the Monitor wants to arrange a person, but a large number of people, the human brain is difficult to cope with, so she found 18 lay, let him program computer solution.

Format input Format

Enter the first line of two integers n,m indicates that there are n students, at most, only to M-students. (1<=m<=n)
Next 2*n line, each two lines represent a classmate's information (if the classmate has no child node, there is only one row).

The first line of each classmate two integer p,s, indicating that the classmate of the weight of p, the number of child nodes s; ( -100<=p<=100)
The second line is an integer representing the number of child nodes of the classmate.

The monitor's number must be 1.

For 20% data 1<=n<=10
For 60% data 1<=n<=100
For 100% data 1<=n<=1000

Output format

Outputs an integer that represents the maximum value of the weight value.

Example 1 sample input 1[copy]
8 5100 22 379 24 5109 36 7 8100 0100 0100 0101 0108 0
Sample output 1[Copy]
518
Hint Source

181818181818

. He sen "on the rational organization of data "
#include <bits/stdc++.h>using namespacestd;#definell __int64#defineESP 1E-13Const intn=2e3+Ten, m=1e6+ +, inf=1e9+Ten, mod=1000000007;intDp[n][n];vector<int>V[n];intW[n];inta[n],hh;intSi[n];intDfsintx) {a[hh++]=x; SI[X]=1;  for(intI=0; I<v[x].size (); i++) {Si[x]+=DFS (V[x][i]); }    returnsi[x];}voidInitintXinty) {     for(intI=0; i<=x;i++) {dp[i][0]=0;  for(intt=2; t<=y;t++) Dp[i][t]=-inf; }}intMain () {intx,y,z,i,t; HH=1; scanf ("%d%d",&x,&y);  for(i=1; i<=x;i++) {scanf ("%d%d",&w[i],&t);  while(t--) {scanf ("%d",&z);        V[i].push_back (z); }} DFS (1);    Init (x, y);  for(i=x;i>=1; i--)    {         for(t=1; t<=y;t++) {Dp[i][t]=max (Dp[i][t],max (dp[i+1][t-1]+w[a[i]],dp[i+Si[a[i]]        [t])); }    }    intans=-inf;  for(i=0; i<=y;i++) ans=max (ans,dp[1][i]); printf ("%d\n", ans); return 0;}

Vijos 1642 The task of the monitor 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.