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.
Think of a guild as a horizontal slice: choose a vertex , then take all descendants of on one fixed depth level.
If that slice only goes through one child subtree of , then choosing was pointless. The exact same set is obtained from that one child. So those pairs should not create new guilds.
Keep pushing the top vertex downward while the desired depth is reachable through exactly one child. You stop either at the target vertex itself, giving a singleton, or at a vertex where at least two children can reach that depth.
So every distinct non-singleton guild corresponds to exactly one pair where and at least two children of have subtree height reaching depth .
For a fixed vertex , child contributes to target depth iff . Thus the number of new guilds from is the number of depth levels covered by at least two child-subtree height intervals.
We need count distinct sets of the form:
So yeah, it is just a subtree sliced by one global depth level.
The Trap
There are up to possible pairs , but many of them describe the same set.
Example: if has only one child branch that reaches the target depth, then the guild from is identical to the guild from that child. Choosing the higher vertex added absolutely nothing. Classic fake complexity nonsense.
Suppose we want depth . For vertex , look at which children of have descendants at depth .
All singleton guilds are distinct, so they contribute exactly .
Now we only need count the non-singleton guilds.
Canonical Form
Take any non-empty guild. If it is not a singleton, start from its chosen vertex and target depth .
If only one child of reaches depth , move down into that child. The set does not change. Repeat.
Eventually, one of two things happens:
In case 2, the set has LCA , so no other vertex can be the canonical source of the same non-singleton set. Therefore every distinct non-singleton guild corresponds to exactly one pair such that at least two children of contain vertices at depth .
So the answer is:
Counting Levels For One Vertex
For a child of , what depths can it contribute to?
The subtree of contains some vertex at every depth from down to its maximum descendant depth. Since trees are connected downward, a deepest path passes through every intermediate depth.
Let:
Since , child contributes to all depths in:
For vertex , all child intervals start at the same point. A depth is covered by at least two children exactly when the second-largest value among is at least that depth.
So if the two largest child maximum depths are , then the contributing depths are:
There are:
such depths, if has at least two children. If it has fewer than two children, it contributes .
That is the whole problem. Compute subtree maximum depths, grab the second largest child maximum for every vertex, add it up, then add .
Algorithm
Why This Is Correct
Every guild is a slice of a subtree at some depth. If the slice passes through only one child subtree, shifting the root to that child gives the exact same set. Repeating this makes every guild either a singleton or a slice from a vertex where at least two child subtrees participate.
Singletons are exactly the sets , so there are of them.
For non-singletons, the canonical vertex is the LCA of all vertices in the guild. That makes the representation unique. A vertex creates one distinct non-singleton guild for every depth level reached by at least two of its children.
A child reaches exactly the depth levels from through . Since all those intervals share the same left endpoint, the number of levels covered by at least two children is determined by the second largest . That count is .
Thus summing this value over all vertices and adding counts every distinct guild exactly once.
Complexity
Each edge is processed a constant number of times.
per test case, with total over all tests. Memory is also .
#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;
cin >> n;
vector<vector<int>> children(n + 1);
vector<int> parent(n + 1, 0), depth(n + 1, 0), mx(n + 1, 0);
for (int i = 2; i <= n; i++) {
cin >> parent[i];
children[parent[i]].push_back(i);
depth[i] = depth[parent[i]] + 1;
}
for (int i = 1; i <= n; i++) mx[i] = depth[i];
for (int v = n; v >= 2; v--) {
mx[parent[v]] = max(mx[parent[v]], mx[v]);
}
ll ans = n; // all singleton guilds
for (int v = 1; v <= n; v++) {
int best1 = -1, best2 = -1;
for (int c : children[v]) {
if (mx[c] > best1) {
best2 = best1;
best1 = mx[c];
} else if (mx[c] > best2) {
best2 = mx[c];
}
}
if (best2 != -1) {
ans += best2 - depth[v];
}
}
cout << ans << '\n';
}
return 0;
}#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;
cin >> n;
vector<vector<int>> children(n + 1);
vector<int> parent(n + 1, 0), depth(n + 1, 0), mx(n + 1, 0);
for (int i = 2; i <= n; i++) {
cin >> parent[i];
children[parent[i]].push_back(i);
depth[i] = depth[parent[i]] + 1;
}
for (int i = 1; i <= n; i++) mx[i] = depth[i];
for (int v = n; v >= 2; v--) {
mx[parent[v]] = max(mx[parent[v]], mx[v]);
}
ll ans = n; // all singleton guilds
for (int v = 1; v <= n; v++) {
int best1 = -1, best2 = -1;
for (int c : children[v]) {
if (mx[c] > best1) {
best2 = best1;
best1 = mx[c];
} else if (mx[c] > best2) {
best2 = mx[c];
}
}
if (best2 != -1) {
ans += best2 - depth[v];
}
}
cout << ans << '\n';
}
return 0;
}