Data Structures & AlgorithmsVery hardCodeforces Div2 C · ICPC regional

A tree has these parent→child edges: 66→83, 25→54, 66→58, 25→74, 63→51, 63→25, 54→12, 51→39, 12→34, 14→66, 83→63. The distance between two nodes is the number of edges on the path between them. How many unordered pairs of distinct nodes are at distance exactly 2?

  1. A. 13
  2. B. 24
  3. C. 26
  4. D. 11

Correct answer: A. 13

Count each unordered pair once: enumerate the pairs and tally their distances, then read off distance 2. Counting ordered pairs, or the cumulative count up to 2, doubles or inflates the answer.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic