Codeforces Round #656 (Div. 3)E. Directing Edges(拓扑排序+构造dag图)
2021-04-08 16:26
标签:过程 front stand 而且 limit code 再处理 main seve 给你\(n\)个定点\(m\)条边,这\(m\)条边中有有向边也有无向边。 当\(t=0\)时,输入边代表的是无向边。 当\(t=1\)时,输入的边代表的是\(x-y\)的有向边。 要你将所有的无向边变为有向边后整个图是无环的(有向无环图)。 有向边时建边而且更新入度,无向边时只存边不更新入度。 然后在拓扑排序时将无向边插进去,由于在插无向边的时候没有影响到拓扑排序的顺序,所以插入后并不会影响原来拓扑排序的有无环性。 为什么拓扑排序时插入无向边不影响整个拓扑排序? 因为当我们插入时,没有影响到其他任何边入度变化,也不会因为插入导致后面拓扑排序的顺序改变。 本题根据有向完成拓扑排序后,只需要将无向再处理一下。按拓扑排序的顺序从小到大即可(因为这样就不会影响拓扑排序的顺序,从而说明仍然可以按照原来的拓扑排序去走。因为你把边放在了从前到后,回忆拓扑排序过程,增加这条边不会影响之前所有点的入度,因此拓扑排序不变。而当这个点被去除后,新增加的边也被删除了,因此也不影响后面) E. Directing 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 t independent test cases. Input The first line of the test case contains two integers n and m (2≤n≤2?105, 1≤m≤min(2?105,n(n?1)2)) — the number of vertices and the number of edges in the graph, respectively. The next m lines describe edges of the graph. The i-th edge is described with three integers ti, xi and yi (ti∈[0;1], 1≤xi,yi≤n) — the type of the edge (ti=0 if the edge is undirected and ti=1 if the edge is directed) and vertices this edge connects (the undirected edge connects vertices xi and yi and directed edge is going from the vertex xi to the vertex yi). 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,yi) there are no other pairs (xi,yi) or (yi,xi)). It is guaranteed that both sum n and sum m do not exceed 2?105 (∑n≤2?105; ∑m≤2?105). Output 参考博客:https://blog.csdn.net/Satur9/article/details/107436886 Codeforces Round #656 (Div. 3)E. Directing Edges(拓扑排序+构造dag图) 标签:过程 front stand 而且 limit code 再处理 main seve 原文地址:https://www.cnblogs.com/waryan/p/13377820.html题目大意
思路
当所有点都入过队后说明是构造出来的无环图,否则本身就有环。#include
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a graph consisting of n vertices and m 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.
The first line of the input contains one integer t (1≤t≤2?104) — the number of test cases. Then t test cases follow.
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 m 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.
https://blog.csdn.net/woshixuxiran/article/details/107428917
上一篇:Java String
文章标题:Codeforces Round #656 (Div. 3)E. Directing Edges(拓扑排序+构造dag图)
文章链接:http://soscw.com/index.php/essay/72938.html