Lawyers
InterviewTime limit1sMemory limit512 MB
Given N lawyers and M directed trust edges, decide whether every lawyer has at least one incoming edge and no two lawyers defend each other.
- Level
Medium4 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
lawyers are charged with fraud. The lawyers want to defend each other so that all of them are acquitted.
A lawyer can be defended only by a lawyer they trust. Trust is given as pairs , meaning lawyer trusts lawyer . Only in that case can lawyer defend lawyer .
The lawyers are highly skilled, so anyone defended by at least one lawyer is acquitted without exception. However, if defends and defends , both are found guilty, because such a pair is highly suspicious.
For each pair , decide whether lawyer defends lawyer . Determine whether it is possible for every lawyer to be acquitted.
Input
The first line contains and . ()
From the second line onward, the -th of the next lines contains two distinct integers and . This means lawyer can defend lawyer .
No ordered pair appears more than once in the input.
Output
If every lawyer can be defended by at least one lawyer and there is no pair of lawyers who defend each other, print YES on the first line.
Otherwise, print NO on the first line.