Problem 272
Count Bridges
Hard · 30 pts · Graph, Depth-First Search, Biconnected Component
Problem
In an undirected graph, a bridge is an edge whose removal increases the number of connected components, meaning some pair of nodes that was connected becomes disconnected. Count the bridges of the given graph. An edge on a cycle is never a bridge, because the rest of the cycle still connects its ends. Tarjan's bridge-finding algorithm uses one DFS: record each node's discovery time and the lowest discovery time reachable from its subtree using one back edge. A tree edge u → v is a bridge when low[v] > disc[u].
Input
First line: integers N (nodes, labelled 1..N) and M (edges). Next M lines: an edge u v.
Output
Print the number of bridges.
Constraints
1 <= N <= 100000; 0 <= M <= 200000; no self-loops or repeated edges
Example input
5 5 1 2 2 3 3 1 3 4 4 5
Example output
2
Explanation
Nodes 1, 2 and 3 form a triangle, so none of its edges is a bridge. Removing 3-4 cuts off nodes 4 and 5, and removing 4-5 cuts off node 5. So there are 2 bridges.