Add a bunch of other problems.

This commit is contained in:
2025-12-15 13:40:39 -03:00
parent 97ebdbf890
commit c4acd23d19
172 changed files with 25644 additions and 79 deletions

View File

@@ -91,46 +91,60 @@ void solve()
int n, k;
cin >> n >> k;
vi depth(n);
vi sums(n);
V<bool> leaf(n, true);
sums[0] = 1;
vvi graph(n);
nrep(i, 1, n) {
int a;
cin >> a;
a--;
depth[i] = depth[a] + 1;
sums[depth[i]]++;
leaf[a] = false;
int j;
cin >> j;
graph[j - 1].push_back(i);
}
int maxdepth = oo;
rep(i, n) {
if (leaf[i]) {
rmin(maxdepth, depth[i]);
vi sums;
int minimal = oo;
function<void(int, int)> calc = [&](int i, int d) {
if (d >= sums.size()) {
sums.emplace_back();
}
}
vi dp(n + 1);
dp[0] = 1;
sums[d]++;
d++;
rep(i, maxdepth) {
for (int j = n; j >= sums[i]; j--) {
dp[j] |= dp[j - sums[i]];
}
}
rep(i, n + 1) {
if (dp[i] && i <= k && n - i <= n - k) {
cout << maxdepth + 1 << '\n';
if (graph[i].empty()) {
rmin(minimal, d);
return;
}
for (auto j : graph[i]) {
calc(j, d);
}
};
calc(0, 0);
V<V<int>> dp(sums.size() + 1, V<int>(k + 1, -1));
dp[0][k] = n - k;
int maximal = 0;
nrep(i, 1, minimal + 1) {
int cost = sums[i - 1];
rep(j, k + 1) {
if (j + cost <= k) {
rmax(dp[i][j], dp[i - 1][j + cost]);
}
if (dp[i - 1][j] >= cost) {
rmax(dp[i][j], dp[i - 1][j] - cost);
}
if (dp[i][j] >= 0) {
maximal = i;
}
}
}
cout << maxdepth << '\n';
cout << maximal << '\n';
}
int main()