Tree-shaped dp+ (Group backpack | | Binary tree, general tree, conversion between forests) Codevs 1378 elective Courses

Source: Internet
Author: User

Codevs 1378 Elective courses

Time limit: 1 s

space limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

The school implements the credit system. Each compulsory course is subject to a fixed credit and must also receive a corresponding elective course credit. The school opens an elective course for N (n<300), and the number of optional courses per student is given. Students take this m course and pass the examination will be able to obtain the corresponding credit.

In elective courses, some courses can be directly elective, some courses require a certain amount of basic knowledge, you must choose some other courses on the basis of the optional. For example, "Frontpage" must be elective after "Windows Operational basics". We call the "Basics of Windows Operations" a first-class lesson in Frontpage. There is at most one door for direct first-class lessons in each course. Two courses may also have the same first class. Each course has a class number, in turn, the For example:

"See pictures"
Table 1 is 2 of the first courses, 2 is 3, 4 of the first course.   If you choose 3, then both 1 and 2 must have been selected. Your task is to identify a course for yourself, so that you can get the most credits and must meet the principle of first-class priority. It is assumed that there is no time conflict between courses.

Enter a description Input Description

The first line of the input file consists of two integers N, M (in the middle separated by a space) where 1≤n≤300,1≤m≤n.
The following n lines represent one course per line. The class number is 1,2,...,n in turn. Each line has two numbers (separated by a space), the first number is the class number of the course first (if there is no first course, the item is 0), the second number is the course of credit. Credits are positive integers of no more than 10.

Output description Output Description

The output file has only one number, the total number of credits in the actual selected course.

Sample input Sample Input

7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2

Sample output Sample Output

13

Data range and Tips Data Size & Hint

Each test point 1s

Category labels Tags  Dynamic planning

Code one: "Tree-shaped dp+ Group Backpack" method

Basic idea: Because this topic and "Kim's budget plan" compared to a main part of the main parts will also have the condition, then we first put the smallest main parts and accessories to create a backpack, along the branch upward transfer, the small backpack as a large backpack items

#defineN 320#include<iostream>using namespacestd; #include<cstdio>#include<cstring>structedge{intV,last;} Edge[n*N];intHead[n],w[n];intF[n][n];intn,m,t=0;voidAdd_edge (intUintv) {T++; EDGE[T].V=v; Edge[t].last=Head[u]; Head[u]=t;}voidinput () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;++i) {intx; scanf ("%d%d",&x,&W[i]);    Add_edge (X,i); }}voidDfsintk) {//Note the difference between the virtual point 0 and the real point, the root of the virtual point tree has no value, up to the sub-tree can fetch m points, and for the real point tree, his root node must be taken, so the subtree can fetch up to m-1 points * /    intflag=1;/*the real point of the record .*/    if(k==0) flag=0;  for(intI=1; i<=m;++i) f[k][i]=W[K];/*take root node first*/     for(intL=head[k];l;l=edge[l].last) {DFS (EDGE[L].V);/*to update upward with a backpack formed on a subtree*/          for(intj=m;j>=flag;--j) for(intp=0;p <=j-flag;++p) {F[k][j]=max (f[k][j],f[k][j-p]+f[edge[l].v][p]); }    }}intMain () {input (); DFS (0); printf ("%d\n", f[0][m]); return 0;}

Code two: "Tree-shaped dp+ two fork tree, General tree, conversion between forests"

#include <iostream>using namespacestd; #include<cstdio>#include<cstring>#defineN 321structnode{intLch,rch,sum;} Node[n]={0};intn,x,m,w[n]={0};intf[n][n]={0};intcount[n]={0};voidAddintUintv) {/*General tree to binary tree, "left child right brother" principle, V is a child of u, if v cannot do U's left child, then make u left child brother, or U left child Brother brother*/    if(node[u].lch==0) {Node[u].lch=v; }    Else{        intI=Node[u].lch;  while(Node[i].rch) i=Node[i].rch; Node[i].rch=v; }}intRead () {CharSintx=0; S=GetChar ();  while(s<'0'|| S>'9') s=GetChar ();  while('0'<=s&&s<='9') {x=x*Ten+s-'0'; S=GetChar (); }    returnx;}voidinput () {n=read (); m=read ();  for(intI=1; i<=n;++i) {x=read (); w[i]=read ();    Add (x,i); }}voidDfsintk) {    if(Node[k].lch) DFS (Node[k].lch);/*Take care of the kids first.*/    if(Node[k].rch) DFS (Node[k].rch); COUNT[K]=count[node[k].lch]+count[node[k].rch]+1; f[k][1]=W[K];/*In case this is a leaf node .*/     for(intI=0; i<=count[node[k].rch];++i) {F[k][i]=max (F[k][i],f[node[k].rch][i]);/*The current point takes I, which can be taken from the right child of not including K, and may include K for his right child, contained in the equation below, which contains a total of three cases*/         for(intj=0; j<=count[node[k].lch];++j)/*or if the child is taken together, the current K-node will be taken.*/F[k][i+j+1]=max (f[k][i+j+1],f[node[k].rch][i]+w[k]+F[node[k].lch][j]); }}intMain () {input (); DFS (node[0].lch); cout<<f[node[0].lch][m]<<endl;/*Notice the deep search, from 0 of the left child to search, because count[node[0].lch]==m, so can not cout<<count[0][m]. And starting at 0, some of the statements in DFS are meaningless.*/    /*DFS (0); cout<<f[0][m+1]<<endl;*/    return 0;}

Tree-shaped dp+ (Group backpack | | Binary tree, general tree, conversion between forests) Codevs 1378 elective Courses

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.