eolymp
bolt
Try our new interface for solving problems
Problems

Jumbled Trees

Jumbled Trees

You are given an undirected connected graph with n vertices and m edges. Each edge has an associated counter, initially equal to 0. In one operation, you can choose an arbitrary spanning tree and add any value v to all edges of this spanning tree.

Determine if it’s possible to make every counter equal to its target value xi modulo prime p, and provide a sequence of operations that achieves it.

Input

The first line contains three integers n, m and p - the number of vertices, the number of edges, and the prime modulus (1n500, 1m1000, 2p109, p is prime).

Next m lines contain three integers ui, vi, xi each - the two endpoints of the i-th edge and the target value of that edge’s counter (1ui, vin, 0xi < p, uivi).

The graph is connected. There are no loops, but there may be multiple edges between the same two vertices.

Output

If the target values on counters cannot be achieved, print -1.

Otherwise, print t - the number of operations, followed by t lines, describing the sequence of operations. Each line starts with integer v (0v < p) - the counter increment for this operation. Then, in the same line, followed by n - 1 integers e1, e2, ..., en-1 (1eim) - the edges of the spanning tree.

The number of operations t should not exceed 2m. You don’t need to minimize t. Any correct answer within the 2m bound is accepted. You are allowed to repeat spanning trees.

Time limit 1 second
Memory limit 128 MiB
Input example #1
3 3 101
1 2 30
2 3 40
3 1 50
Output example #1
5
60 1 2
81 3 2
20 3 1
50 3 2
51 2 1
Input example #2
2 2 37
1 2 8
1 2 15
Output example #2
3
23 1
15 2
22 1
Input example #3
5 4 5
1 3 1
2 3 2
2 5 3
4 1 4
Output example #3
-1
Source 2022 ICPC, NERC, December 7