Find the Town Judge — Topological Sort

Identify the community star: the member everyone else follows while they follow nobody, or report that no such member exists.

Topological Sort · Easy

The problem

A community app has n members numbered 1 to n. A pair [a, b] in trust means member a follows member b. The star is the one member who follows nobody and is followed by all n - 1 others. Return the star's number, or -1 if there is none. With one member, that member is the star.

The story

A small social app has 1 to 10 members numbered from 1, and members can follow each other. Pair [a, b] means member a follows member b, and no pair repeats. An account is the community star if every other member follows it and it follows nobody. Report the star's number, or -1 if no account qualifies.

What to return

Constraints

Example

Input
n = 3, trust = [[1, 3], [2, 3]]
Output
3

Solve it

More in Topological Sort

All algorithm problems