#P10944. Cave Room Reachability

    ID: 12993 Type: Default 1000ms 256MiB

Cave Room Reachability

Cave Room Reachability

Jiajia and Wind have a cave with \(n\) rooms and one-way corridors connecting some rooms. Every time Wind chooses two distinct rooms \(x\) and \(y\) and asks one of her sons to travel from one room to the other. The son can travel either from \(x\) to \(y\) or from \(y\) to \(x\). Wind promised that every task she might propose is possible. To ease her worries, Jiajia wants to choose a cave in which for every pair of distinct rooms \(u\) and \(v\), at least one of the following holds: there is a path from \(u\) to \(v\) or a path from \(v\) to \(u\). In other words, for every pair of rooms \(u,v\) \(\,(u\ne v)\), the reachability relation is total.

Your task is to determine whether a given cave satisfies this property.

Hint: If we condense the graph into its strongly connected components (SCCs), then the property holds if and only if the condensed directed acyclic graph (DAG) forms a chain. That is, if there are \(k\) SCCs, then after a topological ordering, there must be a direct edge from the \(i\)th SCC to the \((i+1)\)th SCC for all \(1 \le i < k\). If \(k=1\) then the cave trivially satisfies the requirement.

inputFormat

The first line contains two integers \(n\) and \(m\), representing the number of rooms and corridors respectively. The following \(m\) lines each contain two integers \(u\) and \(v\) (1-based indices), indicating a one-way corridor from room \(u\) to room \(v\).

outputFormat

Output a single line containing YES if the cave satisfies the condition (i.e. for every pair of distinct rooms, one is reachable from the other), or NO otherwise.

sample

3 3
1 2
2 3
1 3
YES

</p>