409495: GYM103584 H Sling Trees
Description
In an effort to "go green", a city is investigating planting a forest of sling-trees to replace its current network of highways and stroads.
Under the new transit plan, each of the city's $$$1 \leq N \leq 10^5$$$ neighborhoods would get a single sling-tree. To navigate the network, the citizens would climb into a tree, then repeatedly sling themselves to another tree.
However, not every tree can reach every other tree. Each sling-tree $$$i$$$ has a power $$$p_i$$$ and resistance $$$r_i$$$, where $$$1 \leq p_i, r_i \leq 10^6$$$. To sling from tree $$$i$$$ to tree $$$j$$$, the power of tree $$$i$$$ must not exceed the resistance of tree $$$j$$$. In other words, this requires that $$$p_i \leq r_j$$$.
Numerous groups have raised concerns about this system, specifically worrying about experiencing in-air collisions and getting back home. To reassure the public, the city has made two stipulations for the network.
1) To minimize in-air collisions, each tree will only accept launches from one other tree
2) Citizens will never get stuck in the system (they will be able to return to their starting tree).
Given the $$$N$$$ planned sling-trees and their power and resistances, determine whether the city can keep its promises.
InputThe first line is a single integer, $$$N$$$, giving the number of neighborhoods. The next $$$N$$$ lines consist of two integers each, $$$p_i$$$ and $$$r_i$$$, for the $$$i$$$-th tree.
OutputA single line, "YES" or "NO", whether we can navigate from each sling-tree back to itself.
ExamplesInput3 5 2 9 10 1 6Output
YESInput
3 5 4 8 6 5 10Output
NO