Under construction

Still being built. Things may change

How Many to Be Happy?

Interview

Time limit0.5sMemory limit512 MB

Summary
For each edge find the fewest edges to delete so that it lies in some minimum spanning tree, then sum those counts.
Level

Medium7 of 10

Topics
Minimum spanning tree, Graph, Union-find, Greedy
Solved
No attempts yet

Problem

Let GG be a connected simple undirected graph in which every edge has a weight. Recall the minimum spanning tree (MST) problem. For each edge ee of GG, we ask how much GG must be modified before ee belongs to an MST of GG. Some MST of GG may already contain ee. In that case we say that ee is happy in GG, and we define H(e)=0H(e) = 0. It may also happen that no MST of GG contains ee. In that case we say that ee is unhappy in GG. We may then delete some edges of GG to obtain a connected graph G′G' in which ee is happy. H(e)H(e) is the minimum number of edges to delete from GG so that ee is happy in the resulting graph G′G'.

Figure E.1. A complete graph with 3 nodes.

The graph in Figure E.1 has 3 nodes and 3 edges. Its MST consists of the two edges with weights 1 and 2, so those two edges are happy in the graph. To make the edge of weight 3 happy, delete either one of the two happy edges.

Given a connected simple undirected graph GG, compute H(e)H(e) for every edge ee of GG and print the total sum.

Input

The first line contains two positive integers nn and mm, the number of vertices and the number of edges of the graph, where n≤100n \le 100 and m≤500m \le 500. The vertices are numbered from 1 to nn.

Each of the next mm lines contains three positive integers uu, vv, and ww, describing an edge between vertex uu and vertex vv with weight ww. Every weight is an integer between 1 and 500, inclusive.

The graph is connected, it has no self loop, and no two edges join the same pair of vertices.

Output

Print one integer SS, the sum of H(e)H(e) over all edges ee of GG.

Examples2

  1. Example 1

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

    Input
    7 9
    1 2 8
    1 3 3
    2 3 6
    4 2 7
    4 5 1
    5 6 9
    6 7 3
    7 4 2
    4 6 2
    
    Expected output
    3