using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG{
static
int
minRev(ArrayList edges,
int
n)
{
Dictionary<
int
,
ArrayList> graph =
new
Dictionary<
int
,
ArrayList>();
Dictionary<
int
,
ArrayList> graph_rev =
new
Dictionary<
int
,
ArrayList>();
for
(
int
i = 0;i < edges.Count; i++)
{
int
x = (
int
)((ArrayList)edges[i])[0];
int
y = (
int
)((ArrayList)edges[i])[1];
if
(!graph.ContainsKey(x))
{
graph[x] =
new
ArrayList();
}
graph[x].Add(y);
if
(!graph_rev.ContainsKey(y))
{
graph_rev[y] =
new
ArrayList();
}
graph_rev[y].Add(x);
}
Queue q =
new
Queue();
ArrayList visited =
new
ArrayList();
for
(
int
i = 0; i < n + 1; i++)
{
visited.Add(
false
);
}
q.Enqueue(0);
int
ans = 0;
while
(q.Count != 0)
{
int
curr = (
int
)q.Peek();
visited[curr] =
true
;
int
count = 0;
q.Dequeue();
if
(graph_rev.ContainsKey(curr))
{
for
(
int
i = 0;
i < graph_rev[curr].Count;
i++)
{
if
(!(
bool
)visited[(
int
)(
(ArrayList)graph_rev[curr])[i]])
{
q.Enqueue((
int
)(
(ArrayList)graph_rev[curr])[i]);
}
}
}
if
(graph.ContainsKey(curr))
{
for
(
int
i = 0;
i < ((ArrayList)graph[curr]).Count;
i++)
{
if
(!(
bool
)visited[(
int
)(
(ArrayList)graph[curr])[i]])
{
q.Enqueue((
int
)(
(ArrayList)graph[curr])[i]);
count++;
}
}
}
ans += count;
}
return
ans;
}
public
static
void
Main(
string
[]args)
{
ArrayList edges =
new
ArrayList(){
new
ArrayList(){ 0, 1 },
new
ArrayList(){ 1, 3 },
new
ArrayList(){ 2, 3 },
new
ArrayList(){ 4, 0 },
new
ArrayList(){ 4, 5 } };
int
n = 6;
Console.Write(minRev(edges, n));
}
}