Under construction

Still being built. Things may change

Giant Min Cost Bipartite Matching

Time limit4.2sMemory limit512 MB

Summary
Given a bipartite graph with N vertices per side where every vertex has degree at most 2, find the minimum total cost of a matching of each size from 1 to N.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, Greedy
Solved
No attempts yet

Problem

The bipartite graph has NN vertices on the left side, NN vertices on the right side, and MM edges. Every vertex has degree at most 2. Left and right vertices are each numbered from 11 to NN, and two vertices are joined by at most one edge.

A matching is a set of edges where no two edges share an endpoint. For each size 11, 22, …\ldots, NN, find the minimum cost of a matching of that size.

Input

The first line contains NN and MM, the number of vertices and the number of edges. (1≤N≤500 0001 \le N \le 500\,000, 0≤M≤2N0 \le M \le 2N)

Each of the next MM lines contains (x,y,c)(x, y, c): xx is the left vertex, yy is the right vertex, and cc is the cost of that edge. (1≤x,y≤N1 \le x, y \le N, 1≤c≤1 000 000 0001 \le c \le 1\,000\,000\,000)

Output

Print NN lines. Line ii contains the minimum cost of a matching of size ii. If no such matching exists, print -1.

Examples2

  1. Example 1

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

    Input
    1 0
    
    Expected output
    -1