Codeforces Round #656 (Div. 3)E. Directing Edges(拓扑排序+构造dag图)

题目大意

给你 n n n个定点 m m m条边,这 m m m条边中有有向边也有无向边。

t = 0 t=0 t=0时,输入边代表的是无向边。

t = 1 t=1 t=1时,输入的边代表的是 x − y x-y xy的有向边。

要你将所有的无向边变为有向边后整个图是无环的(有向无环图)。

思路

有向边时建边而且更新入度,无向边时只存边不更新入度。

然后在拓扑排序时将无向边插进去,由于在插无向边的时候没有影响到拓扑排序的顺序,所以插入后并不会影响原来拓扑排序的有无环性。

为什么拓扑排序时插入无向边不影响整个拓扑排序?

因为当我们插入时,没有影响到其他任何边入度变化,也不会因为插入导致后面拓扑排序的顺序改变。

本题根据有向完成拓扑排序后,只需要将无向再处理一下。按拓扑排序的顺序从小到大即可(因为这样就不会影响拓扑排序的顺序,从而说明仍然可以按照原来的拓扑排序去走。因为你把边放在了从前到后,回忆拓扑排序过程,增加这条边不会影响之前所有点的入度,因此拓扑排序不变。而当这个点被去除后,新增加的边也被删除了,因此也不影响后面)
当所有点都入过队后说明是构造出来的无环图,否则本身就有环。

#include
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl '\n'
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;
//#define int long long
#define lson rt<<1
#define rson rt<<1|1
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> PII;
const int maxn = 2e5 + 10;

int n,m;
vector<int>e1[maxn],e2[maxn];
int deg[maxn];
vector<pii >ans;
int vis[maxn];

void topo(){
    queue<int>q;
    while(!q.empty())q.pop();
    for(int i=1;i<=n;++i){
        if(deg[i]==0){
            q.push(i);
        }
    }
    int cnt=0;
    while(!q.empty()){
        int now=q.front();q.pop();
        vis[now]=1;
        ++cnt;
        for(auto v:e1[now]){
            --deg[v];
            if(deg[v]==0&&vis[v]==0){
                q.push(v);
            }
            ans.push_back({now,v});
        }
        for(auto v:e2[now]){
            if(vis[v]==0){
                ans.push_back({now,v});
            }
        }

    }
    if(cnt==n){
        cout<<"YES\n";
        for(auto i:ans){
            cout<<i.first<<" "<<i.second<<endl;
        }
    }else{
        cout<<"NO\n";
    }

}

void solve(){
    cin>>n>>m;
    ans.clear();
    for(int i=0;i<=n;++i){
        deg[i]=0;
        e1[i].clear();e2[i].clear();
        vis[i]=0;
    }
    int t,x,y;
    for(int i=1;i<=m;++i){
        cin>>t>>x>>y;
        if(t==0){
            e2[x].push_back(y);
            e2[y].push_back(x);
        }else{
            e1[x].push_back(y);
            ++deg[y];
        }
    }
    topo();

}


int main(){
    int __;
    cin>>__;
//    scanf("%d",&__);
    while(__--){
        solve();
    }

}

E. Directing Edges
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.

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.

参考博客:https://blog.csdn.net/Satur9/article/details/107436886
https://blog.csdn.net/woshixuxiran/article/details/107428917

你可能感兴趣的:(题解)