Problem:
Cycle Detection for a Directed Graph

Find a cycle in a directed graph G(V, E).

Input

A directed graph G is given in the following format:

|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1

|V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target nodes of i-th edge (directed).

Output

Print 1 if G has cycle(s), 0 otherwise.

Constraints

1 ≤ |V| ≤ 100

0 ≤ |E| ≤ 1,000

si ≠ ti

Sample Input 1

3 3
0 1
0 2
1 2

Sample Output 1

0

Sample Input 2

3 3
0 1
1 2
2 0

Sample Output 2

1

Solution in C# (judge verdict: Accepted)



using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;

namespace GraphSolve {
    static class Program {
        const int mod = (int)1e9 + 7;
        static List<int>[] grf;
        static bool[] b, h;
        static int ans = 0;
        static void Main() {

            Sc sc = new Sc();
            int[] s = sc.Ia;
            grf = new List<int>[s[0]];
            b = new bool[s[0]];
            h = new bool[s[0]];
            for (int i = 0; i < s[0]; i++) { grf[i] = new List<int>(); }
            for (int i = 0; i < s[1]; i++) {
                int[] e = sc.Ia;
                grf[e[0]].Add(e[1]);
            }
            for (int i = 0; i < s[0] && ans == 0; i++) {
                if (!b[i]) { Func(i, i); }
            }
            Console.WriteLine("{0}", ans);

        }
        static void Func(int a, int g) {
            b[a] = true;
            h[a] = true;
            for (int i = 0; i < grf[a].Count; i++) {
                int nv = grf[a][i];
                if (h[nv]) { ans = 1; }
                else if (!b[nv]) { Func(nv, g); }
            }
            h[a] = false;
        }
    }
}
public class Sc {
    private string[] a;
    private int n = -1, m = 0;
    public int I { get { if (n == -1 || n == a.Length - 1) { n = -1; a = Console.ReadLine().Split(); } n++; return int.Parse(a[n]); } }
    public long L { get { if (n == -1 || n == a.Length - 1) { n = -1; a = Console.ReadLine().Split(); } n++; return long.Parse(a[n]); } }
    public double D { get { if (n == -1 || n == a.Length - 1) { n = -1; a = Console.ReadLine().Split(); } n++; return double.Parse(a[n]); } }
    public string S { get { if (n == -1 || n == a.Length - 1) { n = -1; a = Console.ReadLine().Split(); } n++; return a[n]; } }
    public int[] Ia { get { if (n != -1 && n != a.Length - 1) { m = n; n = -1; return Array.ConvertAll(a.Skip(m + 1).ToArray(), int.Parse); } return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } }
    public int[] Ia2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), int.Parse); } }
    public long[] La { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } }
    public long[] La2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), long.Parse); } }
    public double[] Da { get { return Array.ConvertAll(Console.ReadLine().Split(), double.Parse); } }
    public string[] Sa { get { return Console.ReadLine().Split(); } }
}