An early and quite sophisticated version of what we now call a parachute is described in Leonardo's Codex Atlanticus (ca. 1485). Leonardo's parachute consisted of a sealed linen cloth held open by a pyramid-shaped wooden structure.
Linked rings
Skydiver Adrian Nicholas tested Leonardo's design more than 500 years later. For this, a modern lightweight structure tied Leonardo's parachute to the human body. We want to use linked rings, which also provide hooks for the sealed linen cloth. Each ring is made of flexible and strong material. Rings can be easily linked together as every ring can be opened and re-closed. A special configuration of linked rings is the chain. A chain is a sequence of rings in which each ring is only connected to its (at most two) neighbours, as illustrated below. This sequence must have a start and an end (rings that are connected to at most one other ring each). Specifically, a single ring is also a chain.
Other configurations are clearly possible, since a ring can be linked to three or more other rings. We say that a ring is critical if after opening and removing it, all remaining rings form a set of chains (or there are no other rings left). In other words, there can be nothing but chains left.
Example
Consider the 7 rings in the next figure, numbered from to . There are two critical rings. One critical ring is : after its removal, the remaining rings form chains , and . The other critical ring is : after its removal, the remaining rings form chains , and . If we remove any other ring, we do not obtain a set of disjoint chains. For example, after removing ring : although we have that is a chain, the linked rings , , , and do not form a chain.
Statement
Your task is to count the number of critical rings in a given configuration that will be communicated to your program.
At the beginning, there are a certain number of disjoint rings. After that, rings are linked together. At any given time, you can be asked to return the number of critical rings in the current configuration. Specifically, you have to implement three routines.
Init(N)
— it is called exactly once at the beginning to communicate that there are disjoint rings numbered from to (inclusive) in the initial configuration.Link(A, B)
— the two rings numbered and get linked together. It is guaranteed that and are different and not already linked directly; apart from this, there are no additional conditions on and , in particular no conditions arising from physical constraints. Clearly,Link(A, B)
andLink(B, A)
are equivalent.CountCritical()
— return the number of critical rings for the current configuration of linked rings.
Example
Consider our figure with rings and suppose that they are initially unlinked. We show a possible sequence of calls, where after the last call we obtain the situation depicted in our figure.
Call | Returns |
---|---|
Init(7) | |
CountCritical() | 7 |
Link(1, 2) | |
CountCritical() | 7 |
Link(0, 5) | |
CountCritical() | 7 |
Link(2, 0) | |
CountCritical() | 7 |
Link(3, 2) | |
CountCritical() | 4 |
Link(3, 5) | |
CountCritical() | 3 |
Link(4, 3) | |
CountCritical() | 2 |
Input Specification
Your program must read the input in the following format:
- line : :
- lines :
-1
to invokeCountCritical
A B
parameters toLink
Output Specification
Output the answer to each CountCritical
query on a separate line.
Sample Input
7 13
-1
1 2
-1
0 5
-1
2 0
-1
3 2
-1
3 5
-1
4 3
-1
Sample Output
7
7
7
7
4
3
2
Subtask 1 [20 points]
- .
- The function
CountCritical
is called only once, after all the other calls; the functionLink
is called at most times.
Subtask 2 [17 points]
- .
- The function
CountCritical
is called only once, after all the other calls; the functionLink
is called at most times.
Subtask 3 [18 points]
- .
- The function
CountCritical
is called at most times; the functionLink
is called at most times.
Subtask 4 [14 points]
- .
- The functions
CountCritical
andLink
are called, in total, at most times.
Subtask 5 [31 points]
- .
- The functions
CountCritical
andLink
are called, in total, at most times.
Comments