Under construction

Still being built. Things may change

Sweet Potatoes for Breakfast

Time limit3sMemory limit512 MB

Summary
Find the minimum cut that separates root 1 from every leaf, then output the squared length of the cycle if it can stay on the root side of some minimum cut.
Level

Medium7 of 10

Topics
Graph, Tree, DFS
Solved
No attempts yet

Statement

Junpyo has gained a lot of weight. His body fat is 25%, which counts as obese. To eat a balanced diet, Junpyo started eating a sweet potato every morning. After discovering the real taste of sweet potatoes, he decided to grow his own sweet potato field and dig up the best one every morning.

Sweet potato graph

A sweet potato plant has an above-ground part and an underground part, and Junpyo is only interested in the underground part. He calls the graph that abstracts the underground part of a sweet potato a sweet potato graph.

A sweet potato graph is a connected undirected graph that can be divided into five kinds of components. Each edge has an edge strength cc.

  1. Root of the sweet potato graph

    This is the only vertex of the sweet potato graph connected to the above-ground part. This vertex is always vertex 1.

  2. Hard roots

    The hard roots form a tree rooted at vertex 1, the root of the sweet potato graph. Any two vertices are connected, and there is exactly one path between them.

  3. Tuber (sweet potato)

    This is the edible part of what people usually call a sweet potato. A tuber is connected to one of the ends of the hard roots. If a tuber has NN vertices, it is drawn as a convex NN-gon. In other words, it is a simple cycle.

    The mass of a tuber is the square of the number of vertices in it. For example, if a tuber has 5 vertices, its mass is 52=255^2=25.

  4. Thin roots

    Thin roots hang from every vertex of a tuber except the vertex connected to the hard roots, and they form a tree.

  5. Root tips

    These are the leaf vertices of the sweet potato graph, meaning the ends of the hard roots and the thin roots that have no tuber attached. Root tips grip the soil tightly, so when the plant is pulled out, the root tips must stay underground.

To harvest a sweet potato, Junpyo grabs the "root of the sweet potato graph" and pulls it out of the ground with a very strong force in an instant. When the graph is pulled out, it splits into the "vertices pulled out of the ground" and the "vertices left underground". Every vertex must belong to one of the two parts. Every "vertex pulled out of the ground" is connected to the root, and every "vertex left underground" is connected to some root tip.

To split the graph into two parts, some of its edges must be cut. The sweet potato graph is always cut so that the total strength of the cut edges is minimized. If bad luck strikes, an edge of a tuber may be cut, and this is called a sweet potato being broken. Junpyo is a sweet potato master, so he can pull out the graph so that the mass of the tuber pulled out is maximized. He only handles the best sweet potatoes, so broken sweet potatoes are not counted.

Find the total mass of the sweet potatoes Junpyo harvests by pulling out the sweet potato graph.

Input

The first line contains the number of vertices NN and the number of edges MM of the sweet potato graph.

The next MM lines each contain three integers uu, vv, and cc. Each line describes an edge with strength cc that connects vertex uu and vertex vv.

Output

Print the total mass of the sweet potatoes that Junpyo harvests.

Constraints

2≤N≤1,000,0002 \le N \le 1{,}000{,}000

N−1≤M≤2,000,000N - 1 \le M \le 2{,}000{,}000

1≤u,v≤N1 \le u, v \le N, u≠vu \ne v

1≤c≤1,000,000,0001 \le c \le 1{,}000{,}000{,}000

The edges of a tuber never break.

Examples2

  1. Example 1

    Input
    19 19
    1 2 23
    2 3 7
    3 4 6
    4 5 8
    2 6 17
    6 7 4
    7 8 9
    8 9 7
    9 10 4
    10 6 12
    7 11 4
    11 12 1
    11 13 2
    8 14 2
    14 15 1
    9 16 4
    16 17 3
    16 18 3
    10 19 6
    
    Expected output
    25
    
  2. Example 2

    Input
    19 19
    1 2 23
    2 3 7
    3 4 6
    4 5 8
    2 6 13
    6 7 4
    7 8 9
    8 9 7
    9 10 4
    10 6 12
    7 11 4
    11 12 1
    11 13 2
    8 14 2
    14 15 1
    9 16 4
    16 17 3
    16 18 3
    10 19 6
    
    Expected output
    0