---
id: 122
lang: en
translation_of: index.md
source_sha256: 60907bc343264d45bca269fc1bbaed25ce4cf5ea400a8525b557714881b2084d
title: "P2014: Course Selection — Solution"
description: "Add a virtual node 0 with zero credits to form a tree, then run a 0/1 knapsack tree DP, iterating capacities in reverse."
tags: ["OI","DP","Tree DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "选课 (the problem name) rendered as “Course Selection”."
  - "有点0的存在 read as 有(点0)的存在, “because node 0 exists”."
---

## Idea

At first I thought it was topological sort; later, while writing the code, I thought this problem through carefully, and it should be a tree DP.

The main idea is to make a virtual node numbered 0 (this node’s weight (credits) is 0), and then build a tree. Construct the DP equation $F[u][size] = F[son][subsize] + F[u][size - subsize]$. One thing to note here is that this is essentially a 0/1 knapsack on a tree, so when generating the DP table you have to go in reverse. (It <span class="slang" data-zh="坑" data-gloss="kēng, “pit”. To trap or screw someone over; here, a pitfall that cost the author time.">screwed me over</span> for an hour; sigh, I’m still too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span>.) When outputting, because node 0 exists, the knapsack capacity is one unit larger than before.

Also note that in my code I used a dfss to compute the capacity of each sub-knapsack. I don’t know whether it’s necessary; I hope some <span class="slang" data-zh="大佬" data-gloss="dàlǎo, “big guy”. A highly skilled person, an expert; used with admiration.">big shots</span> can point out whether it is.

## Code

```cpp
// P2014.cpp
#include <iostream>
#include <queue>
using namespace std;

const int maxn = 2100;
struct edge
{
    int to, next;
} edges[maxn << 1];
int deg[maxn];
int indeg[maxn];
int head[maxn];
int weight[maxn];
int current = 0;

int n, m;

void addpath(int src, int dst)
{
    edges[current].to = dst;
    edges[current].next = head[src];
    head[src] = current++;
}

int F[maxn][maxn];
int sizes[maxn];

void dfss(int u)
{
    sizes[u] = 1;
    for (int i = head[u]; i != -1; i = edges[i].next)
    {
        dfss(edges[i].to);
        sizes[u] += sizes[edges[i].to];
    }
}

void dfs(int u)
{
    for (int i = 1; i <= m; i++)
        F[u][i] = weight[u];
    for (int i = head[u]; i != -1; i = edges[i].next)
    {
        dfs(edges[i].to);
        int jto = edges[i].to;
        for (int len = m + 1; len >= 2; len--)
            for (int lens = 0; lens < len; lens++)
                F[u][len] = max(F[u][len], F[u][len - lens] + F[jto][lens]);
    }
}

int main()
{
    fill(head, head + maxn, -1);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int k, s;
        cin >> k >> s;
        weight[i] = s;
        addpath(k, i), indeg[i]++, deg[k]++;
    }
    dfss(0);
    dfs(0);
    cout << F[0][m + 1];
    return 0;
}
```
