选课 P2014

This commit is contained in:
LittleSheep 2024-04-14 15:32:15 +08:00
commit 6eef815868
3 changed files with 62 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.bin

53
pick-class/main.cc Normal file
View File

@ -0,0 +1,53 @@
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 300 + 5;
typedef struct {
int left, right;
int score;
} node;
node tree[MAXN];
int mem[MAXN][MAXN];
int N, M;
void insert(int current, int head) {
tree[current].right = tree[head].left;
tree[head].left = current;
}
int dfs(int node, int slots) {
if (node < 0 || !slots) {
return 0;
} else if (!node) { // Root node
return dfs(tree[node].left, slots);
}
if (mem[node][slots] > -1) {
return mem[node][slots];
}
mem[node][slots] = dfs(tree[node].right, slots); // When ignore this node
for (int i = 1; i <= slots; i++) {
int other = dfs(tree[node].left, i - 1) + dfs(tree[node].right, slots - i) +
tree[node].score;
mem[node][slots] = max(mem[node][slots], other);
}
return mem[node][slots];
}
int main() {
// Init
cin >> N >> M;
memset(tree, -1, sizeof tree);
memset(mem, -1, sizeof mem);
for (int i = 1; i <= N; i++) {
int depends, c;
cin >> depends >> c;
insert(i, depends);
tree[i].score = c;
}
// Calculating!
printf("%d", dfs(0, M));
}

8
pick-class/main.in Normal file
View File

@ -0,0 +1,8 @@
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2