Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
A tree has these parent→child edges: 31→49, 31→96, 96→67, 31→36, 36→3, 7→53, 3→14, 7→89, 7→31. 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. 119
- B. 238
- C. 19
- D. 71
Correct answer: A. 119
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: 62→49, 57→75, 99→29, 55→98, 94→62, 80→47, 98→61, 57→55, 61→60, 99→80, 94→57, 55→30, 94→99. The…
- A tree has these parent→child edges: 86→37, 46→74, 91→88, 81→86, 43→12, 64→84, 36→11, 43→64, 91→43, 81→36, 11→46, 37→16, 36→91. The…
- A tree has these parent→child edges: 23→64, 22→81, 23→89, 21→14, 75→49, 21→23, 37→50, 23→75, 50→21, 37→82, 50→22. The distance between two…
- A tree has these parent→child edges: 39→43, 98→31, 98→63, 31→52, 61→32, 61→6, 6→26, 7→98, 98→39, 98→61. The distance between two nodes is…
- A tree has these parent→child edges: 86→7, 4→91, 7→40, 94→12, 91→83, 4→44, 99→3, 12→99, 99→4, 99→86. The distance between two nodes is the…
- A tree has these parent→child edges: 21→51, 13→4, 6→16, 58→27, 89→13, 36→21, 41→58, 13→36, 4→41, 51→9, 13→6, 36→2, 9→18. The distance…