#P9392. 黄玫瑰
黄玫瑰
Problem Description
Given a simple directed acyclic graph with vertices. The vertex weight of vertex is defined as , but the vertex weights are not given.
You need to construct a directed acyclic graph with at most vertices and exactly edges. For each edge in , you must assign some as its edge weight, such that the length of the longest path in is equal to that in .
In , the length of a path is defined as the sum of the weights of all vertices on the path. In , it is defined as the sum of the weights of all edges on the path.
However, none of the are given, so your construction of must satisfy: for any possible positive sequence , the longest path lengths of and must be equal.
Construct such a , or state that it does not exist.
Input Format
The first line contains two integers .
The next lines each contain two integers , indicating that there is a directed edge from vertex to vertex .
Output Format
If there is no satisfying the requirements, output one line containing .
Otherwise, output lines:
The first line contains an integer , the number of vertices in your constructed graph .
The next lines each contain three integers , indicating that in there is an edge from vertex to vertex , and its edge weight equals .
You must ensure that , , and .
If there are multiple valid solutions, you may output any one of them.
7 8
1 2
1 3
2 3
2 6
3 4
5 2
5 7
6 7
7
1 2 1
1 2 5
2 3 2
3 4 3
3 5 6
4 6 4
5 7 7
7 8
1 2
2 3
2 6
4 5
4 7
5 3
7 3
7 6
-1
Hint
[Sample #1 Explanation]
In the figure below, the left is and the right is . Vertices/edges with the same color indicate the same weight:

Note that this is only one possible answer. Other correct answers can also be accepted.
[Sample #2 Explanation]
In the figure below is . There is no valid :

[Constraints]
For all testdata: , , . The given graph is guaranteed to be acyclic and has no multiple edges.
| Subtask ID | Special Property | Score | ||
|---|---|---|---|---|
| , each vertex has indegree at most | ||||
| , each vertex has outdegree at most | ||||
| None | ||||

Translated by ChatGPT 5