This page is still under construction.

Parts of this page are still being built. What you see may change.

Planning the Roadworks

Time limit2sMemory limit512 MB

Summary
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 nn and mm (1≤n≤50001 \le n \le 5000, 1≤m≤1000001 \le m \le 100000), the number of junctions in the town and the number of one-way streets connecting them. The junctions are numbered from 11 to nn.

Each of the next mm lines describes one street. The ii-th of those lines contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i), meaning that a one-way street runs from junction aia_i to junction bib_i. 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, kk. On the following kk lines print the numbers of the streets to close, one per line, in increasing order. Streets are numbered from 11 to mm 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 kk is 00, print only 00 on the first line.

Examples2

  1. Example 1

    Input
    5 6
    1 2
    1 3
    2 3
    3 2
    2 4
    3 4
    
    Expected output
    2
    1
    5
    
  2. Example 2

    Input
    3 3
    1 2
    2 3
    1 3
    
    Expected output
    1
    3