Edge direction - MarisaOJ: Marisa Online Judge

Edge direction

Time limit: 1000 ms
Memory limit: 256 MB
Given an directed graph of $n$ vertices and $m$ edges. Some edges already have direction, but some are not. Direct undirected edges so that the resulting graph is acyclic. ### Input - The first line contains two integers $n,m$. - The next $m$ lines, each line contains three integers $t, u, v$, there is an edge connect $u$ and $v$. If $t = 0$, this edge is not directed. If $t = 1$, this edge is directed from $u$ to $v$. It is guaranteed that the graph contains no self-loop and multiple edge. ### Output - Print `NO` if there is no way to direct edges. Otherwise print `YES`, the next $m$ lines describing edges of the resulting graph (in any order). You can not change the direction of already directed edges. ### Constraints - $1 \le n,m \le 10^5$. - $1 \le u,v\le n$. ### Example Input: ``` 4 5 1 1 2 0 4 3 1 3 1 0 2 3 1 2 4 ``` Output: ``` YES 1 2 3 4 3 1 3 2 2 4 ```