#C7218. Trading Pairs Ordering

    ID: 51065 Type: Default 1000ms 256MiB

Trading Pairs Ordering

Trading Pairs Ordering

Peter is organizing a cryptocurrency exchange platform with n different cryptocurrencies and m trading pair requests. In each trading pair, a trader wants to exchange one cryptocurrency for another. Specifically, if a trader wants to trade currency ai for currency bi, then currency ai must be traded before bi becomes available.

This problem can be modeled as a directed graph \(G=(V,E)\) where the set of vertices is \(V=\{1,2,\dots,n\}\) and for each trading pair there is a directed edge \(a_i \to b_i\). Peter must determine whether there exists an ordering of the cryptocurrencies that respects all trading pairs. In graph theory terms, he must decide whether \(G\) is acyclic and, if so, produce a valid topological ordering of the vertices.

If a valid ordering exists, output "ORDERED" on the first line followed by the order (space-separated) on the second line. Otherwise, output "CYCLIC".

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

  • The first line contains two integers \(n\) and \(m\), the number of cryptocurrencies and the number of trading pairs.
  • The next \(m\) lines each contain two integers \(a_i\) and \(b_i\), representing a trading pair where currency \(a_i\) must be traded before currency \(b_i\).

outputFormat

The output is written to standard output (stdout). If a valid trading order exists, output "ORDERED" on the first line and then a valid ordering of all \(n\) cryptocurrencies on the second line (space-separated). If no valid ordering exists (i.e., the trading pairs form a cycle), output "CYCLIC".

## sample
3 3
1 2
2 3
1 3
ORDERED

1 2 3

</p>