Under construction

Still being built. Things may change

Cactus Folding Plus

Time limit3sMemory limit1024 MB

Summary
Decide whether each edge length can match the distance between its endpoints on a line, and print vertex coordinates if it can.
Level

Medium6 of 10

Topics
DFS, Graph, Dynamic programming
Solved
No attempts yet

Problem

This is the hard version of "Cactus Folding." The two versions differ in the constraint on ll, and the hard version requires you to print the coordinate xix_i of each vertex.

A cactus graph is an undirected graph in which every edge belongs to at most one simple cycle. Hzlo has a cactus graph, and each edge has a length. An edge of length ll that connects vertices uu and vv is written as the ordered triple (u,v,l)(u, v, l). While looking at his graph, Hzlo had this thought:

  • Could some cactus graphs be folded into one dimension?

Before answering that, call a graph foldable into one dimension if it has the following property.

  • Each vertex ii can be assigned a one-dimensional coordinate xix_i so that ∣xu−xv∣=l|x_u - x_v| = l holds for every edge (u,v,l)(u, v, l).

Given a cactus graph, decide whether it is foldable into one dimension.

Input

The first line contains two integers nn and mm, the number of vertices and the number of edges, separated by a space. (1≤n≤1051 \le n \le 10^5, 0≤m≤min⁡(⌊1.5(n−1)⌋,105)0 \le m \le \min(\lfloor 1.5(n-1) \rfloor, 10^5))

Each of the next mm lines describes one edge. The ii-th of these lines contains the two vertices uiu_i and viv_i that the ii-th edge connects, and its length lil_i, separated by spaces. (1≤ui,vi≤n1 \le u_i, v_i \le n, u≠vu \ne v, 0≤li≤5000 \le l_i \le 500)

The graph has no multiple edges and no self-loops, and it is guaranteed to be a cactus graph.

Output

If the graph is foldable into one dimension, print YES on the first line.

On the next line, print nn integers x1,x2,⋯ ,xnx_1, x_2, \cdots, x_n separated by spaces. The ii-th integer is the coordinate of vertex ii, and it must lie in the range [−109,109][-10^9, 10^9]. Any output that satisfies ∣xu−xv∣=l|x_u - x_v| = l for every edge (u,v,l)(u, v, l) is accepted. Under the constraints of this problem, if the graph is foldable, an assignment of integers within [−109,109][-10^9, 10^9] that satisfies all edges always exists.

If the graph is not foldable, print NO on a single line.

Hint

The cactus graphs defined in this problem may not be connected.

Examples2

  1. Example 1

    Input
    6 6
    1 2 2
    2 3 4
    3 4 3
    4 5 2
    2 5 1
    5 6 7
    
    Expected output
    YES
    0 2 -2 1 3 10
    
  2. Example 2

    Input
    6 6
    1 2 2
    2 3 4
    3 4 3
    4 5 2
    2 4 2
    5 6 7
    
    Expected output
    NO