#P13771. [CERC 2021] Regional development

[CERC 2021] Regional development

题目描述

The King has received several complaints that certain regions of his kingdom are economically neglected. The citizens have not seen a single merchant travelling on certain roads between villages in a very long time. To fix this problem and return wealth and prosperity to his kingdom, the King has appointed his royal mathematician to come up with a viable plan of merchant's routes.

The plan will consist of a positive number of merchants travelling along each road in one of the directions. The number of merchants entering a village along the roads should be equal to the number of merchants exiting it. To ensure a somewhat even distribution of merchants throughout the kingdom, the King has requested that the number of merchants travelling along each road should be at least one and less than MM.

The royal mathematician has been summoned by the King to present his findings. His future is uncertain as he has not been able to solve the problem. However, he did make some progress. He found a plan with a valid number of merchants travelling along each road. The only problem is that the incoming and outgoing merchants in the villages do not add up (at least not exactly). Their difference might not be zero for every village, but it is equal to zero modulo MM. He is willing to share his findings with you, if you can write a program that finds a valid plan or reports that it doesn't exist.

输入格式

The first line contains NN, the number of villages, RR, the number of roads and the number MM.

The following RR lines describe the roads with numbers AiA_i, BiB_i and CiC_i that indicate a road between villages AiA_i and BiB_i with CiC_i merchants travelling from AiA_i to BiB_i. Cities are numbered from 1 to NN. There is at most one road between each pair of villages and no road connects a village with itself. The difference between incoming and outgoing merchants in each village is equal to 0 modulo MM.

输出格式

Print the number of merchants travelling along each road. Print them in the same order as they were given in the input and on separate lines. If the merchants travel in the opposite direction with respect to the order of cities that defined a road in the input, indicate this with a negative value (e.g. if there are XX merchants travelling from BiB_i to AiA_i, indicate this with X-X in the ii-th line of output).

If there are multiple solutions, you can output any of them. If no solution exists, print the word "IMPOSSIBLE" in a single line (without the quotes).

4 5 4
1 2 1
2 3 2
4 1 1
2 4 3
3 4 2
2
3
2
-1
3

提示

Input limits

  • 1N10001 \leq N \leq 1000
  • 0R10 0000 \leq R \leq 10\ 000
  • 2M10002 \leq M \leq 1000
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • 0<Ci<M0 < C_i < M