Graph Theory37 sections · 1633 units
Open in Course

Common Mistakes (What to avoid)

(Exclude child correctly)

Mistake: forgetting to exclude child vv when computing up[v]. This makes up[v] include down[v], which you then add again in the final answer. Result: double-counting nodes in vv's subtree. Mistake: using the wrong formula for edge weights. If edges have weights ww, you need down[u] + w(u,v) * size[v], not down[u] + size[v].

The weight multiplies the count. Mistake: not handling the root separately. The root has no parent, so up[root] = 0. Make sure your code initializes this correctly and does not try to access the parent of the root.