예제 #1
0
    public int FindJudge(int N, int[][] trust)
    {
        DiGraph di = new DiGraph(N);

        for (int i = 0; i < trust.Length; i++)
        {
            di.AddEdge(trust[i][0] - 1, trust[i][1] - 1);
        }

        int ansCandidate = -1;

        for (int i = 0; i < di.Count(); i++)
        {
            if (di.OutDeg(i) == 0)
            {
                if (ansCandidate == -1)
                {
                    ansCandidate = i;
                }
                else
                {
                    return(-1);
                }
            }
        }

        if (ansCandidate == -1)
        {
            return(-1);
        }

        for (int i = 0; i < di.Count(); i++)
        {
            bool isTrust = false;
            if (i != ansCandidate)
            {
                foreach (var j in di.Adj(i))
                {
                    if (j == ansCandidate)
                    {
                        isTrust = true;
                        break;
                    }
                }

                if (!isTrust)
                {
                    return(-1);
                }
            }
        }

        return(ansCandidate + 1);
    }