Maximum number of bridges in a path of a given graph | C ++ program to find the maximum number of bridges in any path of the given graph ; Stores the nodes and their connections ; Store the tree with Bridges as the edges ; Stores the visited nodes ; for finding bridges ; for Disjoint Set Union ; for storing actual bridges ; Stores the number of nodes and edges ; For finding bridges ; Function to find root of the component in which A lies ; Doing path compression ; Function to do union between a and b ; If both are already in the same component ; If both have same rank , then increase anyone 's rank ; Function to find bridges ; Initialize in time and low value ; Update the low value of the parent ; Perform DFS on its child updating low if the child has connection with any ancestor ; Bridge found ; Otherwise ; Find union between parent and child as they are in same component ; Function to find diameter of the tree for storing max two depth child ; Finding max two depth from its children ; Update diameter with the sum of max two depths ; Return the maximum depth ; Function to find maximum bridges bwtween any two nodes ; DFS to find bridges ; If no bridges are found ; Iterate over all bridges ; Find the endpoints ; Generate the tree with bridges as the edges ; Update the head ; Return the diameter ; Driver Code ; Graph = > 1 -- -- 2 -- -- 3 -- -- 4 | | 5 -- -- 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1e5 + 5 ; vector < vector < int > > v ( N ) ; vector < vector < int > > g ( N ) ; vector < bool > vis ( N , 0 ) ; vector < int > in ( N ) , low ( N ) ; vector < int > parent ( N ) , rnk ( N ) ; vector < pair < int , int > > bridges ; int n , m ; int timer = 0 ; int find_set ( int a ) { if ( parent [ a ] == a ) return a ; return parent [ a ] = find_set ( parent [ a ] ) ; } void union_set ( int a , int b ) { int x = find_set ( a ) , y = find_set ( b ) ; if ( x == y ) return ; if ( rnk [ x ] == rnk [ y ] ) rnk [ x ] ++ ; if ( rnk [ y ] > rnk [ x ] ) swap ( x , y ) ; parent [ y ] = x ; } void dfsBridges ( int a , int par ) { vis [ a ] = 1 ; in [ a ] = low [ a ] = timer ++ ; for ( int i v [ a ] ) { if ( i == par ) continue ; if ( vis [ i ] ) low [ a ] = min ( low [ a ] , in [ i ] ) ; else { dfsBridges ( i , a ) ; low [ a ] = min ( low [ a ] , low [ i ] ) ; if ( in [ a ] < low [ i ] ) bridges . push_back ( make_pair ( i , a ) ) ; else union_set ( i , a ) ; } } } int dfsDiameter ( int a , int par , int & diameter ) { int x = 0 , y = 0 ; for ( int i g [ a ] ) { if ( i == par ) continue ; int mx = dfsDiameter ( i , a , diameter ) ; if ( mx > x ) { y = x ; x = mx ; } else if ( mx > y ) y = mx ; } diameter = max ( diameter , x + y ) ; return x + 1 ; } int findMaxBridges ( ) { for ( int i = 0 ; i <= n ; i ++ ) { parent [ i ] = i ; rnk [ i ] = 1 ; } dfsBridges ( 1 , 0 ) ; if ( bridges . empty ( ) ) return 0 ; int head = -1 ; for ( auto & i bridges ) { int a = find_set ( i . first ) ; int b = find_set ( i . second ) ; g [ a ] . push_back ( b ) ; g [ b ] . push_back ( a ) ; head = a ; } int diameter = 0 ; dfsDiameter ( head , 0 , diameter ) ; return diameter ; } int main ( ) { n = 6 , m = 6 ; v [ 1 ] . push_back ( 2 ) ; v [ 2 ] . push_back ( 1 ) ; v [ 2 ] . push_back ( 3 ) ; v [ 3 ] . push_back ( 2 ) ; v [ 2 ] . push_back ( 5 ) ; v [ 5 ] . push_back ( 2 ) ; v [ 5 ] . push_back ( 6 ) ; v [ 6 ] . push_back ( 5 ) ; v [ 6 ] . push_back ( 3 ) ; v [ 3 ] . push_back ( 6 ) ; v [ 3 ] . push_back ( 4 ) ; v [ 4 ] . push_back ( 4 ) ; int ans = findMaxBridges ( ) ; cout << ans << endl ; return 0 ; }