Under construction

Still being built. Things may change

LaLa and Spirit Summoning

Time limit3sMemory limit1024 MB

Summary
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

LaLa\color{blue}{\text{LaLa}}'s younger sister LiLi\color{purple}{\text{LiLi}} is helping LaLa\color{blue}{\text{LaLa}} cast the spirit summoning magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}.

While LaLa\color{blue}{\text{LaLa}} was asleep, LiLi\color{purple}{\text{LiLi}} had already built a prototype of the spirit to summon. The spirit consists of NN magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joints which allow any magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars attached to them to freely move around them, and MM magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars of various colors, each of which connects two magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} 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, LaLa\color{blue}{\text{LaLa}} has a far higher standard than LiLi\color{purple}{\text{LiLi}}. Of course, LaLa\color{blue}{\text{LaLa}} was not satisfied with LiLi\color{purple}{\text{LiLi}}'s work whatsoever. LaLa\color{blue}{\text{LaLa}} would like to fabulize the prototype by getting rid of some magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars so that

  1. the spirit is beautiful, which means there should not be two magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars of the same color present, and
  2. 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 magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars. Note that the minimum always exists as she can always eliminate all magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} 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 LaLa\color{blue}{\text{LaLa}}.

Input

The input describes the prototype spirit made by LiLi\color{purple}{\text{LiLi}} and is given in the following format:

NN MM

u_0u\_0 v_0v\_0 c_0c\_0

u_1u\_1 v_1v\_1 c_1c\_1

⋮\vdots

u_M−1u\_{M-1} v_M−1v\_{M-1} c_M−1c\_{M-1}

where NN is the number of magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joints, numbered from 00 to N−1N-1, MM is the number of magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars, and for each integer 0≤i<M0 \le i < M, the ii-th magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bar has color c_ic\_i and connects the magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joint u_iu\_i and v_iv\_i.

The input satisfies the following constraints:

  • All the numbers in the input are integers.
  • 2≤N≤2002 \le N \le 200
  • 0≤M≤1 0000 \le M \le 1\,000
  • 0≤u_i<v_i<N0 \le u\_i < v\_i < N and 0≤c_i<M0 \le c\_i < M for all integers 0≤i<M0 \le i < M

Note that there can be multiple magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars connecting the same pair of magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joints.

Output

The output should be a single integer equal to the degree of freedom of the spirit fabulized by LaLa\color{blue}{\text{LaLa}}.

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 EE be an assignment of planar coordinates (we'll call this an embedding) to all magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joints of a spirit. Note that such an embedding can be identified with an element in R2N\mathbb{R}^{2N} by concatenating all coordinates, where NN is the number of magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} joints.

Let C(E)C(E) be the set of embeddings continuously reachable from EE as an element of R2N\mathbb{R}^{2N} while preserving edge lengths. i.e. for each element E′E' of C(E)C(E) and each magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} bars of the spirit connecting magic joints uu and vv, the euclidean distance between uu and vv must be the same in EE and E′E'.

The degree of freedom of EE is the minimum non-negative integer kk such that there exists a continuous bijective mapping F:D→C(E)F:D \rightarrow C(E) where DD is a connected subset of Rk\mathbb{R}^k.

The degree of freedom of a spirit is the maximum degree of freedom over all such embeddings EE.

The following illustrate the spirit fabulized by LaLa\color{blue}{\text{LaLa}} along with one of the optimal embedding and the mapping FF for each sample tests in order.

  1. k=5k = 5, D=R2×\[0,2π)×R2D = \mathbb{R}^2 \times \[0, 2\pi) \times \mathbb{R}^2

    F:(x_0,x_1,x_2,x_3,x_4)↦⟨(x_0,x_1),(x_0,x_1)+(cos⁡x_2,sin⁡x_2),(x_3,x_4)⟩F: (x\_0, x\_1, x\_2, x\_3, x\_4) \mapsto \langle(x\_0, x\_1), (x\_0, x\_1) + (\cos{x\_2}, \sin{x\_2}), (x\_3, x\_4)\rangle

    The following illustrates the 5 degrees of freedom associated with each variables.

  2. k=3k = 3, D=R2×\[0,2π)D = \mathbb{R}^2 \times \[0, 2\pi)

    F:(x_0,x_1,x_2)↦⟨(x_0,x_1),(x_0,x_1)+(cos⁡x_2,sin⁡x_2),(x_0,x_1)+(cos⁡(π3+x_2),sin⁡(π3+x_2))⟩F: (x\_0, x\_1, x\_2) \mapsto \langle(x\_0, x\_1), (x\_0, x\_1) + (\cos{x\_2}, \sin{x\_2}), (x\_0, x\_1) + (\cos{(\frac{\pi}{3} + x\_2)}, \sin{(\frac{\pi}{3} + x\_2)})\rangle

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

  3. k=4k = 4, D=R2×(((0,2]×\[0,2π))∪({0}×\[0,π)))D = \mathbb{R}^2 \times (((0, 2] \times \[0, 2\pi)) \cup (\lbrace 0 \rbrace \times \[0, \pi)))

    F:(x_0,x_1,x_2,x_3)↦⟨P_0,P_0+x_22P_1+1−x_224P_2,P_0+x_2P_1,P_0+x_22P_1−1−x_224P_2⟩F: (x\_0, x\_1, x\_2, x\_3) \mapsto \langle P\_0, P\_0 + \frac{x\_2}{2} P\_1 + \sqrt{1 - \frac{x\_2^2}{4}} P\_2, P\_0 + x\_2 P\_1, P\_0 + \frac{x\_2}{2} P\_1 - \sqrt{1 - \frac{x\_2^2}{4}} P\_2\rangle

    where P_0=(x_0,x_1),P_1=(cos⁡x_3,sin⁡x_3)P\_0 = (x\_0, x\_1), P\_1 = (\cos{x\_3}, \sin{x\_3}) and P_2=(sin⁡x_3,−cos⁡x_3)P\_2 = (\sin{x\_3}, -\cos{x\_3}).

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

  4. k=6k = 6, D=R2×\[0,2π)4D = \mathbb{R}^2 \times \[0, 2\pi)^4

    F:(x_0,x_1,x_2,x_3,x_4,x_5)↦⟨P_0,P_1,P_2,P_3,P_4⟩F: (x\_0, x\_1, x\_2, x\_3, x\_4, x\_5) \mapsto \langle P\_0, P\_1, P\_2, P\_3, P\_4 \rangle

    where P_0=(x_0,x_1)P\_0 = (x\_0, x\_1) and P_i=P_i−1+(cos⁡x_i+1,sin⁡x_i+1)P\_i = P\_{i-1} + (\cos{x\_{i + 1}}, \sin{x\_{i + 1}}) for all integers 1≤i≤41 \le i \le 4.

    The following illustrates the 6 degrees of freedom associated with each variables.

Examples4

  1. Example 1

    Input
    3 3
    0 1 0
    0 2 0
    1 2 0
    
    Expected output
    5
    
  2. Example 2

    Input
    3 3
    0 1 0
    0 2 1
    1 2 2
    
    Expected output
    3
    
  3. Example 3

    Input
    4 4
    0 1 0
    1 2 1
    2 3 2
    0 3 3
    
    Expected output
    4
    
  4. Example 4

    Input
    5 4
    0 1 0
    1 2 1
    2 3 2
    3 4 3
    
    Expected output
    6