using System;
using System.Collections.Generic;
class GfG {
// Perform dfs from node 'd' and mark all reachable nodes
static void dfs(List<int>[] adj, int d, bool[] visited) {
visited[d] = true;
foreach (int neighbor in adj[d]) {
if (!visited[neighbor]) {
dfs(adj, neighbor, visited);
}
}
}
// Build adjacency list while skipping the edge (c, d)
static List<int>[] constructAdj(int V, int[,] edges, int c, int d) {
var adj = new List<int>[V];
// Initialize each adjacency list
for (int i = 0; i < V; i++) {
adj[i] = new List<int>();
}
// Get the number of edges
int E = edges.GetLength(0);
for (int i = 0; i < E; i++) {
int a = edges[i, 0];
int b = edges[i, 1];
// Skip the edge that we are testing for bridge
if ((a == c && b == d) || (a == d && b == c))
continue;
adj[a].Add(b);
adj[b].Add(a);
}
return adj;
}
// Check if edge (c, d) is a bridge
static bool isBridge(int V, int[,] edges, int c, int d) {
// Build the graph without the edge (c, d)
var adj = constructAdj(V, edges, c, d);
// Track visited nodes during DFS
bool[] visited = new bool[V];
// Start dfs from one end of the removed edge
dfs(adj, c, visited);
// If the other end is not reachable, it's a bridge
return !visited[d];
}
static void Main() {
// Number of vertices
int V = 4;
// Graph edges (undirected)
int[,] edges = {{ 0, 1 },{ 1, 2 },{ 2, 3 }};
// Edge to check if it’s a bridge
int c = 1, d = 2;
// Output whether the edge is a bridge
Console.WriteLine(isBridge(V, edges, c, d) ? "true" : "false");
}
}