VIJOSP1180: Course Selection

Source: Internet
Author: User

Describe

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:

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.

Format input Format

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 format

The output file has only one number per line. The first line is the total number of credits for the actual selected course.

Input:

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

Output:

13

Binary tree to multi-fork tree + Tree DP

#include <cstdio>#include<cstring>#include<vector>#include<algorithm>using namespacestd;Const intmaxn=305; Vector<int>MP[MAXN];intn,m;intSCORE[MAXN];intLCH[MAXN],RCH[MAXN];intDP[MAXN][MAXN];intDfsintUintW) {    if(u==-1)return 0; if(w<=0)return 0; if(dp[u][w]!=-1)returnDp[u][w]; DP[U][W]=dfs (RCH[U],W);//2 The right son of a fork tree is a sibling node in a multi-fork tree, selecting only the sibling nodes     for(intI=1; i<=w;i++) Dp[u][w]=max (Dp[u][w],dfs (lch[u],i-1) +dfs (rch[u],w-i) +Score[u]); returndp[u][w];}intMain () {memset (DP,-1,sizeof(DP)); memset (LCH,-1,sizeof(LCH)); memset (RCH,-1,sizeof(RCH)); scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++)    {        intPre; scanf ("%d%d",&pre,&Score[i]);    Mp[pre].push_back (i); }    //Multi-fork Tree Change binary tree     for(intI=0; i<=n;i++)    {        if(Mp[i].size () >0) {Lch[i]=mp[i][0]; intnow=mp[i][0];  for(intj=1; J<mp[i].size (); j + +) {Rch[now]=Mp[i][j]; now=Mp[i][j]; }}} printf ("%d\n", DFS (0, m+1));//contains number No. 0 nodes    return 0;}

VIJOSP1180: Course Selection

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.