402858: GYM100923 B Por Costel and the Algorithm
Description
Even though he isn't a student of computer science, Por Costel the pig has started to study Graph Theory. Today he's learning about Bellman-Ford, an algorithm that calculates the minimum cost path from a source node (for instance, node 1) to all the other nodes in a directed graph with weighted edges. Por Costel, utilizing his scarce programming knowledge has managed to scramble the following code in C++, a variation of the Bellman-Ford algorithm:
You can notice a lot of deficiencies in the above code. In addition to its rudimentary documentation, we can see that Por Costel has stored this graph as an array of edges (the array ). An edge is stored as the triplet signifying an edge that spans from to and has weight . But even worse is the fact that the algorithm is SLOW!
As we want our hooved friend to walk away with a good impression about computer science, we want his code to execute as FAST as possible. In order to do so, we can modify the order of edges in the array so that the while loop executes a small number of times.
Given a directed graph of nodes and edges, you are asked to produce an ordering of the edges such that the Bellman-Ford algorithm written by Por Costel should finish after at most two iterations of the while loop(that is, the program should enter the while loop at most twice).
InputThe first line of the file algoritm.in will contain an integer , the number of test cases.
Each of the test cases has the following format: on the first line, there are two numbers and (), the number of nodes and the number of edges in the graph respectively.
The next lines describe the edges, each containing three integers signifying there is an edge from node to node with weight ()
It is guaranteed that node has at least one outgoing edge.
The graph may contain self loops and/or multiple edges.
OutputThe output file algoritm.out should contain lines representing the answers to each test case.
For each test case you should output a permutation of numbers from to , representing the order of the edges you want in Por Costel's array of edges .
The edges are considered indexed by the order in which they are given in the input (the -th edge read is the edge with index ).
If there are multiple solutions, you are allowed to print any of them.
ExampleInput1Output
4 4
1 2 1
3 4 2
2 3 3
1 3 1
1 4 2 3