#P9392. 黄玫瑰

    ID: 10286 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创Special JudgeO2优化洛谷月赛

黄玫瑰

Problem Description

Given a simple directed acyclic graph GG with nn vertices. The vertex weight of vertex ii is defined as wiw_i, but the vertex weights are not given.

You need to construct a directed acyclic graph GG' with at most 2×n2\times n vertices and exactly nn edges. For each edge in GG', you must assign some wiw_i as its edge weight, such that the length of the longest path in GG' is equal to that in GG.

In GG, the length of a path is defined as the sum of the weights of all vertices on the path. In GG', it is defined as the sum of the weights of all edges on the path.

However, none of the wiw_i are given, so your construction of GG' must satisfy: for any possible positive sequence [w1,,wn][w_1,\ldots,w_n], the longest path lengths of GG and GG' must be equal.

Construct such a GG', or state that it does not exist.

Input Format

The first line contains two integers n,mn,m.

The next mm lines each contain two integers x,yx,y, indicating that there is a directed edge from vertex xx to vertex yy.

Output Format

If there is no GG' satisfying the requirements, output one line containing 1-1.

Otherwise, output n+1n+1 lines:

The first line contains an integer NN, the number of vertices in your constructed graph GG'.

The next nn lines each contain three integers x,y,zx,y,z, indicating that in GG' there is an edge from vertex xx to vertex yy, and its edge weight equals wzw_z.

You must ensure that 0N2×n0\leq N\leq 2\times n, 1x,yN1\leq x,y\leq N, and 1zn1\leq z\leq n.

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 GG and the right is GG'. 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 GG. There is no valid GG':


[Constraints]

For all testdata: 1n200001\leq n\leq 20000, 1m3×1051\leq m\leq 3\times 10^5, 1x,yn1\leq x,y\leq n. The given graph is guaranteed to be acyclic and has no multiple edges.

Subtask ID nn\leq mm\leq Special Property Score
Subtask 1\text{Subtask 1} 50005000 49994999 m=n1m=n-1, each vertex has indegree at most 11 1818
Subtask 2\text{Subtask 2} m=n1m=n-1, each vertex has outdegree at most 11 1919
Subtask 3\text{Subtask 3} 2020 5050 None 2020
Subtask 4\text{Subtask 4} 50005000 1000010000 2121
Subtask 5\text{Subtask 5} 2000020000 3×1053\times 10^5 2222

Translated by ChatGPT 5