#P8819. Interstellar Counterattack
Interstellar Counterattack
Interstellar Counterattack
In the current round of the interstellar war, our side has established n bases in the universe, connected by m one‐way wormholes. Each wormhole is considered to "belong" to the destination base. Due to enemy strikes, some wormholes have been destroyed, but may later be repaired by our two types of special repair teams. Type A teams repair a specific wormhole, while Type B teams repair all damaged wormholes owned by a base. A base is considered operational if at least one of its wormholes is repaired and useable.
Our latest spatial technology allows our warships to teleport instantly through the wormholes for precise strikes. In order to counterattack at the optimal moment, the high command is monitoring two conditions for every base:
- Counterattack capability: If starting from a base, it is possible to traverse through available wormholes (possibly using the same wormhole or base multiple times) infinitely many times (i.e. there exists a cycle reachable from that base), then that base can perform a counterattack.
- Continuous traversal capability: To minimize energy-mass loss during base-to-base transitions, a base can achieve continuous traversal if and only if it has exactly one available wormhole leaving it.
An ideal moment for a counterattack is reached if every base is both counterattack-capable and continuous traversal-capable.
You are given the current status of the wormhole network. All wormholes provided in the input are available and functional. Determine whether this is a moment for a counterattack.
Note: Since the network is under continuous enemy assault and subsequent repairs, the input reflects the real-time state of available wormholes. In this problem, a wormhole connects a base u to a base v (directed from u to v). The condition for continuous traversal is evaluated based solely on the number of available wormholes leaving each base.
Mathematical Formulation:
Let \( outdeg(u) \) be the number of available wormholes that start at base \( u \). Then the base \( u \) has continuous traversal capability if and only if:
[ outdeg(u) = 1 ]
Furthermore, a base \( u \) is counterattack-capable if and only if there exists a cycle reachable from \( u \) in the directed graph. In a finite directed graph where every base has exactly one outgoing edge, it is guaranteed that any path will eventually enter a cycle.
The input is structured so that you are given n and m, followed by m wormholes. Determine if every base meets both conditions. If yes, print YES
; otherwise, print NO
.
inputFormat
The first line contains two integers n and m (1 ≤ n, m ≤ 105), representing the number of bases and the number of available wormholes, respectively.
Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n), meaning there is a directed wormhole from base u to base v.
It is guaranteed that the wormhole network reflects the current state of available connections.
outputFormat
Print a single line with YES
if every base is both counterattack-capable and continuous traversal-capable; otherwise, print NO
.
sample
3 3
1 2
2 3
3 1
YES