Detect Cycle in a Directed Graph
Halvard Build runs jobs for its customers, and a job can be declared to need another job finished first. Those declarations point one way only: saying that packaging waits for testing says nothing about testing waiting for packaging. If the declarations ever come round in a circle, no job in that circle can start and the whole run stalls, so the scheduler refuses the plan up front. A job is even allowed to declare itself as its own prerequisite, which stalls immediately.
Task: Print whether the plan contains a circle of prerequisites.
Input
The first line holds two integers separated by a space: n, the number of jobs, labelled 0 to n-1, and m, the number of declarations. Each of the next m lines holds two integers u v, meaning job v waits for job u. m may be 0. A pair may name the same job twice.
Output
One line holding exactly Yes or No. Yes means some run of declarations leads from a job back to that same job. Circles anywhere in the plan count, including among jobs nothing else points at.
Example:
Input:
3 3
0 1
1 2
2 0
Output:
Yes
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in