There is a maze that is formed by connecting
- Each corridor forms a connection between two distinct rooms.
- No two corridors will connect the same pair of rooms.
- Each room will have at least 1 corridor that connects to it.
One difficulty in navigating through this maze is that the lights are all out, so you cannot see where you are.
One way to move through the maze is to place your right hand at some point on the wall of the starting room and walk forward through the corridors and other rooms without ever taking your hand off the wall. If you walk through the maze in this way, you will end up returning to the original room. The path that you follow will depend upon where you place your right hand in the starting room, since that will determine which corridor you take first.
Given the structure of the maze, you need to answer
Input Specification
The first line of input contains the integer
The next
For example, if the corridor information for room 3 4 2 7
, this means that there are 3 corridors connecting room
The next line contains an integer
The final
Let
Subtasks
- For 2 of the 15 marks available,
, , and . - For another 3 of the 15 marks available,
, , and .
Output Specification
Output
Sample Input
6
1 3
2 5 4
1 1
3 2 5 6
2 4 2
1 4
6
1
2
3
4
5
6
Output for Sample Input
2
5
2
3
5
5
Explanation for Output for Sample Input
The following picture corresponds to one arrangement of the sample input.
Rooms 1 and 3 are connected by one corridor, and it is traversed twice while leaving and returning to the same room, regardless of where we start in rooms 1 or 3.
For room 2, we start at the position A
and move counter-clockwise, we will go into the corridors connecting
For room 4, we may start at position B
, move counter-clockwise, and then traverse corridors
Starting at positions C
and D
, in rooms 5 and 6 respectively will yield a reordering of the same path traversed as we found starting from A
in room 2, which yields a total of 5 traversals of corridors in these cases.
Comments