problems / 178

Critical Infrastructure Links

medium bridges dfs graphs 1000 ms · 256 MB

Critical Infrastructure Links

A city's infrastructure consists of n hubs connected by m bidirectional communication links. A link is critical (or a bridge) if its removal would disconnect at least one hub from the rest of the network.

Your task is to identify all critical links and, for each one, determine which two regions would be separated.

Input

The first line contains two integers n and m (1 ≤ n ≤ 10^4, 0 ≤ m ≤ 10^4), the number of hubs and links.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), representing a bidirectional link between hubs u and v. There are no multiple edges or self-loops.

Output

For each critical link (in the order they appear in the input), print its two endpoints separated by a space, with the smaller endpoint first.

If there are no critical links, print nothing.

Notes

A critical link is an edge whose removal increases the number of connected components. A bridge is an edge that appears on every path between its two endpoints.

In the first example, the graph forms a simple path 1—2—3, so both edges are critical. Edge (1, 2) is a bridge, and edge (2, 3) is a bridge.

Example 1

Input

3 2
1 2
2 3

Output

1 2
2 3

Example 2

Input

4 4
1 2
2 3
3 4
4 1

Output

Hints · reveal one at a time if you're stuck

Hint 1

An edge is a bridge exactly when no other path joins its two endpoints. In a DFS tree, what does a back edge tell you about the existence of such a path?

Hint 2

Use a single DFS with low-link values: for each vertex, track the smallest discovery time reachable from its subtree using tree edges and at most one back edge. A tree edge is a bridge when the child's low value is above the parent's discovery time.

Hint 3

Record a bridge flag for each input edge index during the DFS, then print the flagged edges in input order with the smaller endpoint first. Remember the graph may be disconnected, so start a DFS from every unvisited vertex.

report an issue Prefer your own editor? sooks prep 178 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.