This page is still under construction.

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

Graph bridges

Interview

Time limit1sMemory limit256 MB

Summary
Find every bridge in a connected undirected graph and print them sorted by endpoint.
Level

Medium4 of 10

Topics
DFS, Graph
Solved
No attempts yet

Problem

Given a graph, find every bridge and print them.

A bridge is an edge whose removal splits the graph into two or more parts. In other words, removing it increases the number of connected components.

Input

The first line has the number of vertices VV and the number of edges EE. (1≤V≤100 0001 \le V \le 100\,000, 1≤E≤1 000 0001 \le E \le 1\,000\,000)

Each of the next EE lines has two integers AA and BB, meaning that vertex AA and vertex BB are joined. Every edge is undirected.

The graph is always connected, no edge is given more than once, and AA and BB are never equal. Vertices are numbered from 11 to VV.

Output

Print the number of bridges KK on the first line.

On each of the next KK lines print one bridge in the form A B with A<BA < B. Order the bridges by AA in increasing order, and by BB when AA ties. Print each edge only once. If KK is 0, print only the first line.

Examples7

  1. Example 1

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

    Input
    2 1
    1 2
    
    Expected output
    1
    1 2
    
  3. Example 3

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

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

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

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

    Input
    13 14
    1 2
    2 3
    2 10
    2 11
    3 4
    4 5
    5 3
    10 12
    11 13
    5 6
    6 7
    7 8
    8 9
    9 6
    
    Expected output
    7
    1 2
    2 3
    2 10
    2 11
    5 6
    10 12
    11 13