Problem 173
Find the Town Judge
Easy · 10 pts · Graph, Array, Hash Table
Problem
A town has N people labelled 1 to N, and M pairs "a b" say that person a trusts person b. The town judge, if one exists, trusts nobody, and is trusted by every other person. There can be at most one such person. Print the judge's label, or -1 if there is no judge. Count for every person how many people trust them and whether they trust anyone. The judge is the person trusted by N - 1 others who trusts no one. With N = 1 and no pairs, person 1 is the judge.
Input
First line: integers N and M. Next M lines: a pair a b.
Output
Print the judge's label, or -1.
Constraints
1 <= N <= 1000; 0 <= M <= 10000; a != b; no repeated pairs
Example input
3 2 1 3 2 3
Example output
3
Explanation
Persons 1 and 2 both trust person 3, and person 3 trusts nobody. Person 3 is trusted by everyone else (N - 1 = 2 people), so the judge is 3.