Connections
Time limit3sMemory limit512 MB
Given a strongly connected directed graph, run two specified BFS traversals to build a set of 2n kept roads and print the rest in input order.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
Hard times have come to Byteland. Quantum computing is going mainstream and Qubitland is about to occupy Byteland. Byteland does not have enough money for this war, so its king, Byteman 0x0B, decided to reform the road system and cut expenses.
Byteland has cities connected by one way roads, and these roads let you travel from any city to any other city. No two roads cross outside the cities, and no other roads exist. The roads are one way because each of them carries a barrier halfway that opens in one direction only. The barriers are there to make enemies waste time when they pick the wrong way.
The reform abandons some of the roads so that exactly roads remain. The king's advisers think roads are enough to keep the ability to travel from any city to any other city. Whether fewer roads would also be enough, they do not know. What is left is choosing which roads to abandon.
Input
The input holds several test cases. The first line contains the number of test cases.
The first line of each test case contains and , the number of cities and the number of roads (, ). Each of the next lines contains two integers and describing a road from city to city (, ). The given roads are guaranteed to let you travel from any city to any other city. For every pair of cities and there is at most one road from to and at most one road from to . An answer always exists. The sum of over all test cases in one input is at most 100,000.
Output
For each test case print exactly lines. Each line holds the source city and the destination city of one abandoned road, separated by a single space.
Many sets of roads can be abandoned, so only the set produced by the following rule counts as correct. Inside one test case, number the roads from 1 to in the order they are given.
- Run a breadth first search from city 1 that follows the roads in their given direction. At the start mark only city 1 as visited and put it in a first in first out queue. Take a city out of the queue and look at the roads that leave in increasing order of road number. If such a road ends at a city that is not visited yet, mark that city as visited, put it in the queue, and keep the road. Call the set of roads kept this way .
- Run the same breadth first search from city 1 on the graph with every road reversed. For a city taken out of the queue, look at the roads that enter in increasing order of road number, and if such a road starts at a city that is not visited yet, mark that city as visited, put it in the queue, and keep the road. Call the set of roads kept this way .
- Let . The set holds at most roads, so add the roads outside to it in increasing order of road number until holds exactly roads.
- Abandon every road that did not end up in , and print those roads in increasing order of road number.