using System;
using System.Collections.Generic;
class Graph
{
static int count;
int V;
int Time;
Dictionary<int, List<int>> graph;
public Graph(int vertices)
{
V = vertices;
graph = new Dictionary<int, List<int>>();
Time = 0;
}
public void AddEdge(int u, int v)
{
if (!graph.ContainsKey(u))
graph[u] = new List<int>();
if (!graph.ContainsKey(v))
graph[v] = new List<int>();
graph[u].Add(v);
graph[v].Add(u);
}
void BridgeUtil(int u, bool[] visited, int[] parent, int[] low, int[] disc)
{
visited[u] = true;
disc[u] = Time;
low[u] = Time;
Time++;
foreach (int v in graph[u])
{
if (!visited[v])
{
parent[v] = u;
BridgeUtil(v, visited, parent, low, disc);
low[u] = Math.Min(low[u], low[v]);
if (low[v] > disc[u])
count++;
}
else if (v != parent[u])
low[u] = Math.Min(low[u], disc[v]);
}
}
public void Bridge()
{
bool[] visited = new bool[V];
int[] disc = new int[V];
int[] low = new int[V];
int[] parent = new int[V];
for (int i = 0; i < V; i++)
{
if (!visited[i])
{
BridgeUtil(i, visited, parent, low, disc);
}
}
}
public static int GetBridgeCount()
{
return count;
}
}
class Program
{
static void Main(string[] args)
{
Graph g1 = new Graph(6);
g1.AddEdge(0, 1);
g1.AddEdge(1, 2);
g1.AddEdge(2, 0);
g1.AddEdge(1, 3);
g1.AddEdge(3, 4);
g1.AddEdge(4, 5);
g1.AddEdge(5, 3);
g1.Bridge();
int count = Graph.GetBridgeCount();
if (count == 0)
Console.WriteLine("Given graph is 2-edge connected");
else
Console.WriteLine("Given graph is not 2-edge connected");
Console.ReadKey();
}
}