eolymp
bolt
Try our new interface for solving problems
Problems

Guards

Guards

The royal castles in Molvania follow the design of king Sane, first of his dynasty. He ruled by divide and conquer. Therefore, all castles are built according to a hierarchical pattern based on interconnected buildings. A building consists of \textit{halls} and \textit{corridors} that connect halls. Initially, a castle consists of only one building (the \textit{main building}). When its population grows, the castle is extended as follows: A new peripheral building is constructed, attached to one of the existing buildings. Like any other building, the new building also consists of halls and corridors. An additional corridor is created to connect a hall in the existing building to a hall in the new building. That corridor is the only way to access the new building. The number of halls in a building is at most \textbf{10}. \includegraphics{https://static.e-olymp.com/content/fc/fc5b9d265e4f0ea70448e4460e5190aed27da9cf.jpg} \textit{\textbf{Figure 1}}: The castle layout of the example provided below. In times of turmoil, the king monitors all corridors by strategically placing guards in halls. He asks you to determine the least number of guards required to monitor all corridors in the castle (as he wants to keep his personal guard as large as possible). Note that since the last fire, there are no doors in the castle, so we can safely assume that a guard placed in a hall can monitor all connecting corridors. \InputFile The input contains a number of castle descriptions. Within a castle, each hall is identified by a unique number between \textbf{1} and \textbf{10000}. Each castle is recursively defined, starting with a description of the main building: \begin{enumerate} \item A line containing three integers, representing the number of halls in this building (\textbf{2} ≤ \textbf{n} ≤ \textbf{10}), the number of corridors in this building (\textbf{1} ≤ \textbf{m} ≤ \textbf{45}), and the number of peripheral buildings that were later attached to this building (\textbf{0} ≤ \textbf{w} ≤ \textbf{10}). \item For each of the m corridors: \end{enumerate} \begin{itemize} \item \begin{itemize} \item A line containing two integers (each ≤ \textbf{10000}), representing the two halls connected by this corridor. Both halls are located inside the current building. \end{itemize} \end{itemize} 3. For each of the \textbf{w} peripheral buildings: \begin{itemize} \item \begin{itemize} \item A line containing two integers, describing the corridor that leads to this peripheral building. The first integer represents a hall in the current building, while the second integer represents a hall in the peripheral building. \item The structure of the peripheral building and any newer buildings that were later attached to it, described by repeating rules \textbf{1} to \textbf{3}. \end{itemize} \end{itemize} The castle is fully connected: any hall is directly or indirectly reachable from any other hall. Corridors with the same start and end hall do not exist, and for every two halls there is at most one corridor between them. \OutputFile For each castle, print a single line containing a positive integer: the minimum number of guards to place in halls such that all corridors in the castle are monitored.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5 8 2
1 2
2 4
3 4
1 3
1 5
2 5
3 5
4 5
1 6
3 3 0
6 7
7 8
8 6
5 10
3 2 2
10 11
10 12
11 13
2 1 0
13 9
11 14
3 2 0
14 15
14 16
Output example #1
8
Source NWERC 2012 - NorthWestern European Regional Championship 2012