## IOI '12 P2 - Parachute Rings

View as PDF

Points: 25 (partial)
Time limit: 2.5s
Memory limit: 256M

Problem type
Allowed languages
C, C++

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.

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) and Link(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
CountCritical() 7
CountCritical() 7
CountCritical() 7
CountCritical() 4
CountCritical() 3
CountCritical() 2

• .
• The function CountCritical is called only once, after all the other calls; the function Link is called at most times.

• .
• The function CountCritical is called only once, after all the other calls; the function Link is called at most times.

• .
• The function CountCritical is called at most times; the function Link is called at most times.

• .
• The functions CountCritical and Link are called, in total, at most times.

• .
• The functions CountCritical and Link are called, in total, at most times.

#### Implementation details

You must implements the subprograms described above using the following signatures.

##### C/C++ programs
void Init(int N);
int CountCritical();

##### Pascal programs
procedure Init(N : LongInt);