CF1385E

Directing Edges

普及+/提高

题目描述

You are given a graph consisting of nn vertices and mm edges. It is not guaranteed that the given graph is connected. Some edges are already directed and you can't change their direction. Other edges are undirected and you have to choose some direction for all these edges.

You have to direct undirected edges in such a way that the resulting graph is directed and acyclic (i.e. the graph with all edges directed and having no directed cycles). Note that you have to direct all undirected edges.

You have to answer tt independent test cases.

输入格式

The first line of the input contains one integer tt ( 1≤t≤2⋅1041 \le t \le 2 \cdot 10^4 ) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and mm ( 2≤n≤2⋅1052 \le n \le 2 \cdot 10^5 , 1≤m≤min(2⋅105,n(n−1)2)1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2}) ) — the number of vertices and the number of edges in the graph, respectively.

The next mm lines describe edges of the graph. The ii -th edge is described with three integers tit_i , xix_i and yiy_i ( ti∈[0;1]t_i \in [0; 1] , 1≤xi,yi≤n1 \le x_i, y_i \le n ) — the type of the edge ( ti=0t_i = 0 if the edge is undirected and ti=1t_i = 1 if the edge is directed) and vertices this edge connects (the undirected edge connects vertices xix_i and yiy_i and directed edge is going from the vertex xix_i to the vertex yiy_i ). It is guaranteed that the graph do not contain self-loops (i.e. edges from the vertex to itself) and multiple edges (i.e. for each pair ( xi,yix_i, y_i ) there are no other pairs ( xi,yix_i, y_i ) or ( yi,xiy_i, x_i )).

It is guaranteed that both sum nn and sum mm do not exceed 2⋅1052 \cdot 10^5 ( ∑n≤2⋅105\sum n \le 2 \cdot 10^5 ; ∑m≤2⋅105\sum m \le 2 \cdot 10^5 ).

输出格式

For each test case print the answer — "NO" if it is impossible to direct undirected edges in such a way that the resulting graph is directed and acyclic, otherwise print "YES" on the first line and mm lines describing edges of the resulted directed acyclic graph (in any order). Note that you cannot change the direction of the already directed edges. If there are several answers, you can print any.

输入输出样例

输入样例 #1

text
4
3 1
0 1 3
5 5
0 2 1
1 1 5
1 5 4
0 5 2
1 3 5
4 5
1 1 2
0 4 3
1 3 1
0 2 3
1 2 4
4 5
1 4 1
1 1 3
0 1 2
1 2 4
1 3 2

输出样例 #1

text
YES
3 1
YES
2 1
1 5
5 4
2 5
3 5
YES
1 2
3 4
3 1
3 2
2 4
NO

说明/提示

Explanation of the second test case of the example:

Explanation of the third test case of the example:

🚀 提交评测

登录并绑定洛谷账号后即可在此在线提交代码并实时评测。

前往登录

💡 题解 (0)

登录后即可撰写并分享您的解题思路。

暂无题解,快来发布全站第一篇题解吧!

💬 题目讨论 (0)

登录后可以发起或参与讨论。

暂无讨论内容。

📊 题目信息

题号CF1385E
难度普及+/提高
时间限制3000 ms
内存限制250 MB
题目来源洛谷题库
算法标签
图论拓扑排序

⚡ 快速操作

在线提交代码在洛谷打开原题 ↗