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 what Alice's and Bob's magic functions compute, based on the code and the data structures involved.
Context Recap
The graph is a tree (since edges < vertices and all vertices reachable from 1).
value[] stores some integer value for each vertex.
The magic function returns a result struct with three fields:
rooted_chain: likely the maximum sum of a chain (path) starting at the current root and going downwards.
unrooted_chain: likely the maximum sum of a chain somewhere in the subtree but not necessarily starting at the root.
subtree_path: likely the maximum sum of any path fully contained in the subtree rooted at root, possibly passing through the root or not.
Alice's magic function
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;
}
Step-by-step reasoning:
Initialize res.subtree_path to -INF (to find max).
For each child:
Recursively get subres.
Update res.subtree_path to the maximum of itself and get_result(subres).
Track the top two largest subres.rooted_chain values among children:
If current child's rooted_chain is larger than , update to old and to child's .
res.rooted_chain
unrooted_chain
rooted_chain
rooted_chain
rooted_chain
Else if it's larger than unrooted_chain, update unrooted_chain.
After processing children, add value[root] to res.rooted_chain.
Interpretation:
rooted_chain is the maximum chain sum starting at root and going down (since we take the max child's rooted_chain and add value[root]).
unrooted_chain is the second largest such chain among children.
subtree_path is the max path sum found in any child's subtree or possibly a path passing through root that combines the two largest child chains (rooted_chain + unrooted_chain).
Because get_result(res) returns max(subtree_path, rooted_chain + unrooted_chain), the function is computing:
The maximum path sum anywhere in the subtree rooted at root.
This path can be:
Entirely in a child's subtree (subtree_path),
Or a path passing through root connecting the two highest child chains (rooted_chain + unrooted_chain).
Conclusion for Alice:
Alice's function computes the maximum path sum in the subtree rooted at root.
This is the classic "maximum path sum in a tree" DP approach, where a path can be any path (not necessarily starting at root), and the path can pass through the root by connecting two child chains.
Bob's magic function
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;
}
Differences from Alice:
Instead of comparing subres.rooted_chain, Bob compares subres.unrooted_chain + value[edge[e].dest].
He updates unrooted_chain and rooted_chain with these values.
Then adds value[root] to rooted_chain.
What does subres.unrooted_chain + value[edge[e].dest] represent?
subres.unrooted_chain is the second best chain in the child's subtree (not necessarily rooted).
Adding value[edge[e].dest] (the child's own value) suggests Bob is trying to build chains starting from the child node itself.
Intuition:
Bob's unrooted_chain and rooted_chain track the top two chains starting at children nodes but including the child's own value explicitly.
Then he adds value[root] to rooted_chain, meaning the chain is extended upwards to the root.
What is Bob computing?
He seems to be computing the maximum path sum starting at the root and going down, but allowing the chain to start at any child node's subtree chain plus the child's own value.
The subtree_path is updated similarly to Alice's, tracking the max path sum in any child's subtree.
The final get_result(res) returns the max of:
The best path in any child's subtree (subtree_path),
Or the sum of the two best chains starting at children plus the root's value.
But the difference is subtle:
Alice uses subres.rooted_chain (chain starting at child),
Bob uses subres.unrooted_chain + value[child].
Given that unrooted_chain is the second best chain in child's subtree, adding value[child] means Bob is considering chains that start somewhere inside child's subtree but not necessarily at child's root, then extending it by child's own value.
This suggests Bob's function is computing:
The maximum path sum starting at the root and going downwards, but allowing the chain to be formed by combining chains that do not necessarily start at the child's root but somewhere inside child's subtree plus child's own value.
Summary of what each function computes:
Function
Meaning of rooted_chain and unrooted_chain
What magic(root) returns
What get_result(magic(1)) returns
Alice
rooted_chain: max chain sum starting at rootunrooted_chain: second max chain sum starting at root's children
Max path sum info in subtree rooted at root
Maximum path sum anywhere in the tree
Bob
unrooted_chain + value[child]: max chain sum starting at some node in child's subtree including childrooted_chain: second max such chain
Max chain sums starting at or below root, but chains can start inside child's subtree
Maximum chain sum starting at root (or subtree chains extended by child's value)
Final interpretation:
Alice's function computes the maximum path sum anywhere in the tree (classic max path sum in tree).
Bob's function computes something more like the maximum chain sum starting at the root, but allowing chains that start inside child's subtree plus child's value, effectively a more complicated chain definition.
Additional notes:
Alice's approach is the standard solution to the "maximum path sum in a tree" problem.
Bob's approach is less standard and seems to be mixing chain definitions, possibly computing the maximum chain sum starting at root but allowing chains that start inside child's subtree plus child's value.
Summary
Person
What their magic function computes
Alice
The maximum path sum anywhere in the subtree rooted at root. This path can be any path in the subtree, possibly passing through root and connecting two child chains.
Bob
The maximum chain sum starting at root (or extended from chains starting inside child's subtree plus child's value), tracking chains that may start deeper in the subtree but are extended upwards. This is a more complex chain definition, not the classic max path sum.
If you want, I can help you with a more detailed explanation or a proof of correctness for Alice's approach!