Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
A tree has these parent→child edges: 8→10, 8→41, 36→5, 52→91, 36→8, 5→33, 91→89, 33→17, 41→53, 36→52. The distance between two nodes is the number of edges on the path between them. Let W be the sum of the distances over all unordered pairs of distinct nodes. What is W?
- A. 340
- B. 170
- C. 20
- D. 90
Correct answer: B. 170
Each edge contributes s·(n−s) to W, where s is the number of nodes on the child side of that edge. Doubling for ordered pairs or summing depths from the root measures other things.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A tree has these parent→child edges: 30→95, 46→34, 25→93, 46→68, 93→46, 47→28, 47→3, 47→25, 28→30. The distance between two nodes is the…
- A tree has these parent→child edges: 42→41, 40→42, 42→93, 89→65, 41→8, 65→21, 93→34, 41→49, 8→89. The distance between two nodes is the…
- A tree has these parent→child edges: 55→41, 8→70, 13→28, 4→95, 95→55, 13→45, 13→8, 41→59, 45→4. The distance between two nodes is the…
- A tree has these parent→child edges: 93→46, 38→97, 89→83, 93→89, 97→20, 38→6, 20→80, 89→38, 80→12. The distance between two nodes is the…
- A tree has these parent→child edges: 47→36, 47→76, 13→97, 23→90, 36→13, 76→60, 97→49, 24→47, 36→32, 23→24. The distance between two nodes…
- A tree has these parent→child edges: 6→27, 27→54, 76→6, 26→22, 52→76, 29→52, 29→57, 26→29. The distance between two nodes is the number of…