Planning the Roadworks
Time limit2sMemory limit512 MB
Given a directed graph, find a lexicographically smallest inclusion-maximal set of edges whose simultaneous removal leaves the reachability relation unchanged.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
A roadworks program has started in Bytetown. Several streets are closed and traffic in the town is limited. Some people say that parts of the town are now completely cut off, but nobody coordinates the roadworks, so there is no way to check.
To get the situation under control, the mayor made Byteman the head of the Department of Computer-Assisted Roadworks Coordination. Work that has already started cannot be stopped halfway, and the roadworks budget still holds money that has to be spent before a strict deadline. The mayor therefore asked Byteman for a list of streets that can be closed on top of the current closures, all at the same time, without limiting travel in the town any further. In other words, if one junction can be reached from another junction now, that has to remain true after every street on the list is closed.
Byteman first tried to find the largest such list and failed. He settled for a list that cannot be extended: adding any other street to it would cut off the access between some pair of junctions that can reach each other now. Write a program that prepares this list.
Input
The first line contains two integers and (, ), the number of junctions in the town and the number of one-way streets connecting them. The junctions are numbered from to .
Each of the next lines describes one street. The -th of those lines contains two integers and (, ), meaning that a one-way street runs from junction to junction . No ordered pair appears more than once in the input. The roadworks started without proper preparation, so nothing can be assumed about the current shape of the street network.
Output
On the first line print the number of streets on the list, . On the following lines print the numbers of the streets to close, one per line, in increasing order. Streets are numbered from to in the order they appear in the input.
If several lists satisfy the conditions, print the lexicographically smallest one. Sort both lists in increasing order and compare them position by position: the list holding the smaller number at the first position where they differ comes first. If is , print only on the first line.