Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The LCS can never be longer than the shortest leaf name. So the first number to compute is , the minimum root-to-leaf path length.
If beauty equals , then every leaf must share some binary subsequence of length . Since a shortest leaf name also has length , that subsequence must be the entire name of every shortest leaf. No wiggle room there.
A clean way to force beauty : for each depth , make every vertex at that depth have the same label. Then every leaf has the same length- prefix, so the LCS reaches the upper bound.
Now the labeling count becomes subset sum. Let be the number of vertices at depth . For depths through , each whole depth group must be either all zero or all one. So the possible number of zeros used in these forced groups is any subset sum of .
For a subset sum , beauty is feasible exactly when the remaining vertices can absorb the leftover labels: and . If no such exists, the answer is always .
Let the depth of the root be .
For a leaf , its name has length . So if
then the answer is at most . You cannot have a common subsequence longer than the shortest string. That upper bound is brutal and immediate.
So the whole problem becomes:
That second bullet sounds suspicious at first, but it is true. More on that after we handle the main test.
What It Means To Reach
Suppose the beauty is . Then there is a binary string of length that is a subsequence of every leaf name.
Now look at any leaf with minimum depth . Its name also has length . A length- subsequence of a length- string must be the whole string. No deletions, no tricks, no funny business.
So all shortest leaf paths must spell exactly the same string .
For longer leaf paths, only has to appear as a subsequence. But we can always think of the -th character of as being matched by some group of vertices, and every root-to-leaf path must hit these groups in order.
The important simplification is this: for checking whether length is possible, it is enough to use the depth layers themselves as those groups.
That means:
Then every leaf has the same prefix of length , so the LCS is at least . Since is already the upper bound, the beauty is exactly .
Why is this not losing anything? If some weird optimal construction matches the subsequence at uneven depths, you can push those matched groups upward. Whenever a vertex at depth at most is skipped, every leaf below it still needs the remaining matches somewhere in its subtree. Move the earliest needed match in that subtree up to this vertex, and free the old matched vertices below it. This preserves the subsequence order and only makes label-count flexibility better. Repeating this leaves the first depth layers as the forced groups. So the normal-looking depth-layer construction is not just cute; it is the right thing to test.
Turning Labels Into Knapsack
Let
To force beauty , each group must be monochromatic. If depth is labeled zero, it uses zeros. If it is labeled one, it uses ones.
Let
The vertices deeper than do not matter for the common prefix/subsequence of length , so they can receive whatever labels are needed to fix the total count.
So we only need to know which zero counts are possible among the first depth groups. This is a plain subset sum over weights
If some subset sum is possible, then:
This is compatible with the global requirement of exactly zeros and ones iff
and
The remaining deeper vertices can absorb the leftovers. That is literally their job here.
If such an exists, answer .
Why Otherwise The Answer Is
We already know the answer cannot exceed .
If fails, we claim is always achievable.
Force only depths to be monochromatic. Then every leaf shares the same prefix/subsequence of length .
Can we always fit the exact number of zeros? Yes.
Before depth , every vertex must have at least one child, otherwise there would be a leaf above depth . Therefore the depth sizes are nondecreasing up to :
When we only force the first layers, all vertices from depth onward are free. In particular, the free pool has size at least , which is at least as large as every forced group size.
Now take the forced group sizes in any order and keep adding whole groups as zeros until adding the next group would pass . The previous sum is within one group size of , and one group size fits inside the free pool. So the free vertices can always fill the remaining zeros. That gives an exact labeling with beauty at least .
Since was impossible, the answer is exactly .
Algorithm
For each test case:
Complexity
The subset sum has total weight at most , so it costs per test case and memory.
With , this is very comfortably fine. No need for bitset wizardry in the easy version.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> depth(n + 1), children(n + 1, 0), cnt(n + 2, 0);
depth[1] = 1;
cnt[1] = 1;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
children[p]++;
depth[i] = depth[p] + 1;
cnt[depth[i]]++;
}
int D = n;
for (int i = 1; i <= n; i++) {
if (children[i] == 0) D = min(D, depth[i]);
}
vector<char> dp(n + 1, 0);
dp[0] = 1;
int sum = 0;
for (int d = 1; d <= D; d++) {
int w = cnt[d];
for (int x = sum; x >= 0; x--) {
if (dp[x]) dp[x + w] = 1;
}
sum += w;
}
bool canFull = false;
for (int x = 0; x <= sum; x++) {
if (dp[x] && x <= k && sum - x <= n - k) {
canFull = true;
break;
}
}
cout << (canFull ? D : D - 1) << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> depth(n + 1), children(n + 1, 0), cnt(n + 2, 0);
depth[1] = 1;
cnt[1] = 1;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
children[p]++;
depth[i] = depth[p] + 1;
cnt[depth[i]]++;
}
int D = n;
for (int i = 1; i <= n; i++) {
if (children[i] == 0) D = min(D, depth[i]);
}
vector<char> dp(n + 1, 0);
dp[0] = 1;
int sum = 0;
for (int d = 1; d <= D; d++) {
int w = cnt[d];
for (int x = sum; x >= 0; x--) {
if (dp[x]) dp[x + w] = 1;
}
sum += w;
}
bool canFull = false;
for (int x = 0; x <= sum; x++) {
if (dp[x] && x <= k && sum - x <= n - k) {
canFull = true;
break;
}
}
cout << (canFull ? D : D - 1) << '\n';
}
}