#K9491. Task Completion with Dependencies

    ID: 38746 Type: Default 1000ms 256MiB

Task Completion with Dependencies

Task Completion with Dependencies

You are given P projects. For each project, you are given its number of tasks and a number of dependency pairs. The tasks are identified by integers from 1 to T, and each dependency is a pair of integers (u, v) meaning that task u must be completed before task v can be started.

Your goal is to decide whether it is possible to complete all tasks in a project without encountering any circular dependencies. Output Yes if the tasks can be completed (i.e. the dependency graph is acyclic) and No if there is a cycle.

Note: There are no duplicate or self-dependencies.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer P, the number of projects.
  • For each project, the first line contains two integers: T (number of tasks) and M (number of dependency pairs).
  • This is followed by M lines, each containing two integers u and v which represent that task u must be completed before task v.

outputFormat

For each project, print a single line to stdout:

  • Yes if all tasks can be completed (i.e. the dependency graph has no cycle), or
  • No if there exists a cycle.
## sample
2
3 2
1 2
2 3
4 4
1 2
2 3
3 4
4 2
Yes

No

</p>