活动人数 十四届蓝桥杯青少年组省赛

This commit is contained in:
LittleSheep 2024-04-14 15:32:42 +08:00
parent 6eef815868
commit 43af4da056
2 changed files with 53 additions and 0 deletions

46
counting-people/main.cc Normal file
View File

@ -0,0 +1,46 @@
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> tree[MAXN];
int amount[MAXN];
int mem[MAXN][2];
int N, Rt;
int dfs(int node, int flag) {
if (mem[node][flag] > -1) {
return mem[node][flag];
}
mem[node][0] = 0;
mem[node][1] = amount[node];
for (int i = 0; i < tree[node].size(); i++) {
int leaf = tree[node][i];
mem[node][0] += max(dfs(leaf, 0), dfs(leaf, 1));
mem[node][1] += dfs(leaf, 0);
}
return mem[node][flag];
}
int main() {
// Init
cin >> N;
memset(mem, -1, sizeof mem);
for (int i = 0; i < N; i++) {
int depends, idx, c;
cin >> depends >> idx >> c;
amount[idx] = c;
if (depends == 0) {
Rt = idx;
} else {
tree[depends].push_back(idx);
}
}
// Calculating!
printf("%d", max(dfs(Rt, 1), dfs(Rt, 0)));
}

7
counting-people/main.in Normal file
View File

@ -0,0 +1,7 @@
6
0 1 2
1 2 4
1 3 3
2 4 3
3 5 2
3 6 4