Programming contests

DEIK Regionális Programozó Csapatverseny, középiskolai kategória, 2013. december 1.

December 1, 2013 10:30 AM – December 1, 2013 3:30 PM

Alternative Arborescence

Given a graph, we define “proper coloring” as coloring of the graph nodes in such a way that no two adjacent nodes have the same color. If we map each color to a positive integer, we can calculate the sum of all colors assigned to the graph.

In this problem, you will be given a tree (connected graph with no simple loops). Can you determine the minimum color sum that can be achieved when the tree is properly colored? The image below shows a proper coloring of the second example tree with a sum of 11:

Input Specification

The input consists of several test cases. Each test case starts with n (1 ≤ n ≤ 10000), the number of nodes in the tree. The next n lines will be of the form “uv1 v2 … vk”, where u is the root of a subtree, and vi's are its children (0 ≤ uvi ≤ n – 1).

Every test case will be followed by a blank line. The input ends with a case where n = 0, which should not be processed.

Output Specification

For each test case, print the minimum sum of colors that can be achieved by some proper coloring of the tree.

Sample Input

  1. 2
  2. 0:
  3. 1: 0
  4. 8
  5. 0: 1 2 3
  6. 1: 4 5
  7. 2:
  8. 3: 6 7
  9. 4:
  10. 5:
  11. 6:
  12. 7:
  13. 0
download as text file

Output for Sample Input

  1. 3
  2. 11
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019