#K61802. Detect Cyclic Dependencies in Libraries

    ID: 31390 Type: Default 1000ms 256MiB

Detect Cyclic Dependencies in Libraries

Detect Cyclic Dependencies in Libraries

Your task is to determine whether a given set of libraries has any cyclic dependency. Each dataset consists of a specified number of libraries and a set of dependency relations between these libraries. A dependency relation A B indicates that library A depends on library B. A cycle occurs if there is a sequence of libraries such that each library depends on the next, and the last one depends back on the first. Use LaTeX for any formulas, e.g. a cycle can be represented as \(A \to B \to \cdots \to A\).

The input will contain multiple datasets. For each dataset, you need to output "Cyclic" if there is at least one cyclic dependency in the dependencies given, or "Acyclic" if no cycles are detected.

Note: Even if there are libraries with no dependencies, they are considered acyclic.

inputFormat

The input is given via standard input (stdin) in the following format:

T
n m
lib1 dep1
lib2 dep2
... (m lines)
... (repeat the above for T datasets)
  • T is the number of datasets.
  • For each dataset, the first line contains two integers n and m where n is the number of libraries (this value is informational) and m is the number of dependency relations.
  • Each of the next m lines contains two strings representing a dependency: the library and the library it depends on.

It is guaranteed that each dependency is given in a separate line and there are no extra spaces.

outputFormat

For each dataset, print a single line to standard output (stdout) containing either "Cyclic" if a cycle is detected, or "Acyclic" if there is no cycle.

## sample
5
3 3
A B
B C
C A
4 4
A B
B C
C D
D A
3 2
A B
B C
2 0
3 2
A B
A C
Cyclic

Cyclic Acyclic Acyclic Acyclic

</p>