From 6eef81586890f940588aa53b2eabf19afbdb456c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 14 Apr 2024 15:32:15 +0800 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=E9=80=89=E8=AF=BE=20P?= =?UTF-8?q?2014?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + pick-class/main.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++ pick-class/main.in | 8 +++++++ 3 files changed, 62 insertions(+) create mode 100644 .gitignore create mode 100644 pick-class/main.cc create mode 100644 pick-class/main.in diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8a0dce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.bin diff --git a/pick-class/main.cc b/pick-class/main.cc new file mode 100644 index 0000000..7941063 --- /dev/null +++ b/pick-class/main.cc @@ -0,0 +1,53 @@ +#include +#include +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)); +} diff --git a/pick-class/main.in b/pick-class/main.in new file mode 100644 index 0000000..d45ab7f --- /dev/null +++ b/pick-class/main.in @@ -0,0 +1,8 @@ +7 4 +2 2 +0 1 +0 4 +2 1 +7 1 +7 6 +2 2