Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-281

Biconnected Components Count

Problem

Count the biconnected components (blocks) of an undirected graph.

Run a DFS with an edge stack and pop a block whenever low[child] >= disc[parent]; every bridge forms its own block and an isolated vertex counts as a block. edges is a list of [u, v] pairs. Return the number of blocks.

Hint

Whenever no earlier ancestor is reachable from the child's subtree, the edges above the current tree edge form one block.

Test cases

case 1
in: [3,[[0,1],[1,2]]]
expected: 2
case 2
in: [3,[[0,1],[1,2],[2,0]]]
expected: 1
case 3
in: [5,[[0,1],[1,2],[2,0],[1,3],[3,4]]]
expected: 3
case 4
in: [4,[[0,1],[1,2],[2,0],[2,3]]]
expected: 2
case 5
in: [3,[]]
expected: 3
Starter codePython
def biconnected_components_count(n, edges):
    # Your code here
    pass

5 test cases · run your code against them in the editor