Cactus Folding Plus
Time limit3sMemory limit1024 MB
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 , and the hard version requires you to print the coordinate 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 that connects vertices and is written as the ordered triple . 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 can be assigned a one-dimensional coordinate so that holds for every edge .
Given a cactus graph, decide whether it is foldable into one dimension.
Input
The first line contains two integers and , the number of vertices and the number of edges, separated by a space. (, )
Each of the next lines describes one edge. The -th of these lines contains the two vertices and that the -th edge connects, and its length , separated by spaces. (, , )
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 integers separated by spaces. The -th integer is the coordinate of vertex , and it must lie in the range . Any output that satisfies for every edge is accepted. Under the constraints of this problem, if the graph is foldable, an assignment of integers within 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.