LaLa and Spirit Summoning
Time limit3sMemory limit1024 MB
Keep at most one bar per color so the remaining spirit has the fewest planar degrees of freedom, and print that count.
- Level
Hard9 of 10
- Topics
- Graph, Greedy, Implementation
- Solved
- No attempts yet
Problem
's younger sister is helping cast the spirit summoning .
While was asleep, had already built a prototype of the spirit to summon. The spirit consists of joints which allow any bars attached to them to freely move around them, and bars of various colors, each of which connects two joints and whose length can be adjusted to any non-negative real number before the summoning (but not after).
When it comes to spirit summoning, has a far higher standard than . Of course, was not satisfied with 's work whatsoever. would like to fabulize the prototype by getting rid of some bars so that
- the spirit is beautiful, which means there should not be two bars of the same color present, and
- the spirit is as easy to control as possible, which means the degree of freedom of the spirit must be minimum over all beautiful spirits obtainable by eliminating some bars. Note that the minimum always exists as she can always eliminate all bars to create a beautiful spirit. See the note below for the exact definition of the degree of freedom.
Write a program that computes the degree of freedom of the spirit fabulized by .
Input
The input describes the prototype spirit made by and is given in the following format:
where is the number of joints, numbered from to , is the number of bars, and for each integer , the -th bar has color and connects the joint and .
The input satisfies the following constraints:
- All the numbers in the input are integers.
- and for all integers
Note that there can be multiple bars connecting the same pair of joints.
Output
The output should be a single integer equal to the degree of freedom of the spirit fabulized by .
Hint
Intuitively, the degree of freedom is the number of axis of motions preserving edge lengths of the spirit embedded on a plane.
More formally, let be an assignment of planar coordinates (we'll call this an embedding) to all joints of a spirit. Note that such an embedding can be identified with an element in by concatenating all coordinates, where is the number of joints.
Let be the set of embeddings continuously reachable from as an element of while preserving edge lengths. i.e. for each element of and each bars of the spirit connecting magic joints and , the euclidean distance between and must be the same in and .
The degree of freedom of is the minimum non-negative integer such that there exists a continuous bijective mapping where is a connected subset of .
The degree of freedom of a spirit is the maximum degree of freedom over all such embeddings .
The following illustrate the spirit fabulized by along with one of the optimal embedding and the mapping for each sample tests in order.
-
,
The following illustrates the 5 degrees of freedom associated with each variables.

-
,
The following illustrates the 3 degrees of freedom associated with each variables.

-
,
where and .
The following figure on the left illustrates the 4 degrees of freedom associated with each variables. Note that the motion associated with the variable is non-rigid. The one on the right illustrates the motion associated with in detail.

-
,
where and for all integers .
The following illustrates the 6 degrees of freedom associated with each variables.
