A village has n people labelled 1..n. You are given a list of one-way trust relations: a pair a b means person a trusts person b.
There may be at most one arbiter. The arbiter is the person who
n - 1 people.Report the arbiter's label, or -1 if no such person exists.
Line 1: two integers n and m (the number of people and the number of trust relations).
The next m lines each contain two integers a b, meaning person a trusts person b.
A single integer: the label of the arbiter, or -1 if there is none.
Example 1
Input
3 3 1 3 2 3 1 2
Expected
3
Explanation
Person 3 trusts nobody and is trusted by persons 1 and 2 (all other 2 people), so 3 is the arbiter.
Example 2
Input
3 3 1 2 2 3 3 1
Expected
-1
Explanation
Everyone trusts someone, so nobody can be the arbiter. The answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →