Locate the Universally Followed Silent Guest
Trees & GraphsAuto-graded · 20 test casesLast updated
Find the one node in a directed relation that everyone points to and that points to nobody, using a linear elimination pass followed by verification.
Find the Universal Sink
A gathering has n guests labeled 0 through n-1. Acquaintance is directional: guest a may know guest b without b knowing a.
You receive the list acquaintances. Each entry [a, b] states that guest a knows guest b. Any ordered pair (a, b) that does not appear in the list means a does not know b.
A guest x is the focal guest if both of these hold:
- Every other guest knows
x. For everyi != x, the pair[i, x]is present. xknows no one. For everyi != x, the pair[x, i]is absent.
Return the label of the focal guest, or -1 if there is none.
Rules and edge cases
- At most one focal guest can exist, so the answer is unique.
- If
n == 1, guest0is trivially the focal guest, because there is nobody else to know or to be known by. - No entry has
a == b. If the same pair appears more than once, it still states a single fact. - Entries are only about guests in the range
0..n-1.
Aim for a solution that does not examine all n * (n - 1) ordered pairs one by one. A linear number of relationship lookups is achievable.
Constraints
1 <= n <= 10^4- Each entry of
acquaintancesis[a, b]with0 <= a, b < nanda != b. acquaintancesmay be empty.- Return an integer in
[0, n-1], or-1.
Examples
Example 1
Input: find_focal_guest(3, [[0, 2], [1, 2]])
Output: 2
Example 2
Input: find_focal_guest(3, [[0, 1], [1, 2], [2, 0]])
Output: -1
Example 3
Input: find_focal_guest(1, [])
Output: 0
Hints
Hint 1
Pick any two different guests a and b and ask whether a knows b. Whichever answer you get, one of the two can be ruled out as the focal guest. Which one in each case?
Hint 2
Keep a running candidate. Compare it against each next guest: if the candidate knows that guest, the candidate is out and the new guest takes over; otherwise the new guest is out.
Hint 3
After the scan only one guest can possibly qualify, but nothing proves they do. Run a verification pass: everyone else must know them, and they must know no one.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Think of the relation as a directed graph where a -> b means "a knows b". The focal guest is a vertex with in-degree n-1 and out-degree 0.
Key observation. For any two different guests a and b, one lookup of "does a know b?" eliminates one of them:
- If yes,
aknows someone, soacannot be focal. - If no, not everyone knows
b, sobcannot be focal.
Elimination pass. Start with candidate = 0. For each guest g from 1 to n-1, check whether candidate knows g. If so, g becomes the new candidate. Otherwise the candidate stays. Each step discards one person, so after the scan at most one guest can still be focal.
Verification pass. Surviving the scan does not prove the candidate is focal; it only proves nobody else can be. So for every other guest g, check that g knows the candidate and the candidate does not know g. If any check fails, return -1. Otherwise return the candidate.
Why it is correct
Invariant: after handling guest i, every guest in 0..i other than the current candidate has been ruled out. When the candidate knows i, the old candidate is ruled out and i replaces it. When the candidate does not know i, then i is ruled out. By induction, if a focal guest exists, it must be the final candidate, and verification accepts it. If none exists, verification must fail for the survivor, so -1 is returned.
Uniqueness: if x and y were both focal, x would have to know y (everyone knows y), but x knows nobody, which is a contradiction.
For n == 1, both loops are empty and guest 0 is returned.
Complexity
- Time: O(n + m), where
mis the number of listed pairs. Building the set costs O(m) and then there are at most about3nlookups. - Space: O(m) for the set of pairs.
Reference solution
from typing import List
def find_focal_guest(n: int, acquaintances: List[List[int]]) -> int:
# Store each "a knows b" fact for constant-time lookup.
relation = {(a, b) for a, b in acquaintances}
# Elimination pass: each comparison removes exactly one guest.
candidate = 0
for guest in range(1, n):
if (candidate, guest) in relation:
# The candidate knows someone, so they cannot be focal.
candidate = guest
# Otherwise `guest` is unknown to the candidate, so they cannot be focal.
# Verification pass: the survivor must satisfy both conditions.
for guest in range(n):
if guest == candidate:
continue
if (guest, candidate) not in relation:
return -1
if (candidate, guest) in relation:
return -1
return candidate