Codeforce 656 Problem E (拓扑排序)

题面

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.

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 input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

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
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.

思路

分析可知,如果有向边已经成环,那么必定不成功,否则我们就可以通过改变调整无向边的方向去使其成为有向无环图,所以最终我们只需要用小学二年级学过的拓扑排序,去求是否存在换,并且把过程中按照出队的顺序去把点放到一个数组里面去就可以了。

代码实现

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005 
#define fi first 
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int ,int> PII;
const int maxn=2e5+10;
    
vector <int > va[maxn];
int u[maxn],v[maxn];
int indu[maxn];
int n,m,cnt[maxn],num;
queue <int > q;

inline void toposort () {
    while (q.size ()) {
        int u=q.front ();
        q.pop ();
        cnt[u]=++num;
        if (va[u].size ()) 
        rep (i,0,va[u].size()-1) {
            indu[va[u][i]]--;
            if (!indu[va[u][i]]) q.push (va[u][i]);
        }
    }
}

int main () {
   int t;
   cin>>t;
   while (t--) {
       cin>>n>>m;
       num=0;
       rep (i,1,n) {
           indu[i]=cnt[i]=0;
           va[i].clear();
       }
       rep (i,1,m) {
           int op;
           cin>>op>>u[i]>>v[i];
           if (op) {
               indu[v[i]]++;
               va[u[i]].pb(v[i]);
           }
       }
       rep (i,1,n) if (!indu[i]) q.push (i);
       toposort ();

       if (num<n) cout<<"NO"<<endl;
       else {
           cout<<"YES"<<endl;
           rep (i,1,m) {
               if (cnt[u[i]]<cnt[v[i]]) cout<<u[i]<<" "<<v[i]<<endl;
               else cout<<v[i]<<" "<<u[i]<<endl;
           }
       }z
   }
     return 0;
}   
原文地址:https://www.cnblogs.com/hhlya/p/13382843.html