int value[MAXVRTX];
typedef struct {
int rooted_chain, unrooted_chain, subtree_path;
} result;
int get_result(result res) {
return max(res.subtree_path, res.rooted_chain + res.unrooted_chain);
}
result magic(int root);
int main() {
// Input the number and values of vertices and call add_edge() to create edges between them.
// Guaranteed that #edges < #vertices and all vertices can be accessed from vertex #1.
printf("%d", get_result(magic(1)));
}
// Code ends
Here is Alice's approach:
result magic(int root) {
result res = {0, 0, -INF}, subres;
for (int e = head[root]; e; e = edge[e].next_idx) {
subres = magic(edge[e].dest);
res.subtree_path = max(res.subtree_path, get_result(subres));
if (res.rooted_chain < subres.rooted_chain) {
res.unrooted_chain = res.rooted_chain;
res.rooted_chain = subres.rooted_chain;
} else if (res.unrooted_chain < subres.rooted_chain)
res.unrooted_chain = subres.rooted_chain;
}
res.rooted_chain += value[root];
return res;
}
And here's Bob's:
result magic(int root) {
result res = {0, 0, -INF}, subres;
for (int e = head[root]; e; e = edge[e].next_idx) {
subres = magic(edge[e].dest);
res.subtree_path = max(res.subtree_path, get_result(subres));
if (res.unrooted_chain < subres.unrooted_chain + value[edge[e].dest]) {
res.rooted_chain = res.unrooted_chain;
res.unrooted_chain = subres.unrooted_chain + value[edge[e].dest];
} else if (res.rooted_chain < subres.unrooted_chain + value[edge[e].dest])
res.rooted_chain = subres.unrooted_chain + value[edge[e].dest];
}
res.rooted_chain += value[root];
return res;
}
What do their functions do respectively?
Assistant
Let's analyze Alice's and Bob's implementations of the magic function in the context of a tree DP problem. The code structure and variable names strongly suggest that the problem is about finding the maximum path sum in a tree, possibly the diameter (maximum sum of values along any path) or a related path problem.
Let's break down what each function does.
Alice's Implementation
result magic(int root) {
result res = {0, 0, -INF}, subres;
for (int e = head[root]; e; e = edge[e].next_idx) {
subres = magic(edge[e].dest);
res.subtree_path = max(res.subtree_path, get_result(subres));
if (res.rooted_chain < subres.rooted_chain) {
res.unrooted_chain = res.rooted_chain;
res.rooted_chain = subres.rooted_chain;
} else if (res.unrooted_chain < subres.rooted_chain)
res.unrooted_chain = subres.rooted_chain;
}
res.rooted_chain += value[root];
return res;
}
What is Alice computing?
rooted_chain: The maximum sum of a path starting at root and going down (i.e., the best single downward path from root).
unrooted_chain: The second-best such path among all children.
subtree_path: The best path sum anywhere in the subtree (not necessarily passing through root).
In the loop:
For each child, she recursively gets the best path sums.
She keeps track of the two best rooted_chain values among the children (the two highest downward paths).
She updates subtree_path to be the best found in any child.
After the loop:
She adds value[root] to rooted_chain (to include the current node's value).
At the end:
The get_result function, called in main, returns the maximum of:
res.subtree_path (best in the subtree)
res.rooted_chain + res.unrooted_chain (the sum of the two best downward paths through root, i.e., the best path passing through root and going down two different branches).
Conclusion: Alice's function correctly computes the maximum path sum in the tree, where a path can go down two branches through the root (i.e., the "tree diameter" in terms of node values).
Bob's Implementation
result magic(int root) {
result res = {0, 0, -INF}, subres;
for (int e = head[root]; e; e = edge[e].next_idx) {
subres = magic(edge[e].dest);
res.subtree_path = max(res.subtree_path, get_result(subres));
if (res.unrooted_chain < subres.unrooted_chain + value[edge[e].dest]) {
res.rooted_chain = res.unrooted_chain;
res.unrooted_chain = subres.unrooted_chain + value[edge[e].dest];
} else if (res.rooted_chain < subres.unrooted_chain + value[edge[e].dest])
res.rooted_chain = subres.unrooted_chain + value[edge[e].dest];
}
res.rooted_chain += value[root];
return res;
}
What is Bob computing?
rooted_chain and unrooted_chain are updated differently.
For each child, he uses subres.unrooted_chain + value[edge[e].dest] to update res.unrooted_chain and res.rooted_chain.
But notice:
subres.unrooted_chain is never updated in the function, so it always remains 0 (from initialization).
Therefore, subres.unrooted_chain + value[edge[e].dest] is just value[edge[e].dest].
So, Bob's code is always comparing and updating with just the values of the child nodes, not the best paths down from them.
After the loop:
He adds value[root] to rooted_chain.
What does this mean?
Bob is only considering the immediate children's values for his two best chains, not the best paths going down from them.
So, for each node, his rooted_chain is value[root] + max(value[child1], value[child2], ...)
His unrooted_chain is the second largest among the value[child].
Conclusion: Bob's function computes the maximum path sum that passes through the root and at most two of its immediate children, i.e., only considers paths of length at most 2 edges (root-child-grandchild is not considered). He does not consider longer paths in the subtree.
Summary Table
Function
What it computes
Alice
The maximum path sum in the tree (the "diameter" in terms of node values), i.e., the best path anywhere, possibly passing through root and going down two branches.
Bob
The maximum sum of root plus up to two of its immediate children (i.e., only considers paths of length 1 or 2 edges, not longer paths).
Final Notes
Alice's approach is correct for the classic "maximum path sum in a tree" problem.
Bob's approach is incorrect for this problem, as he only considers immediate children and ignores longer paths.
If you want more details or a visualization, let me know!