Codeforces Round 656 (Div. 3) E.Directing Edges

题目

题解:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
#define ll long long
#define pii pair<int, int>
#define ld long double
const int maxn = 1e6 + 5, inf = 1e18, maxm = 4e4 + 5, base = 337;
const int N = 1e6;
// const int mod = 1e9 + 7;
// const int mod = 998244353;
const __int128 mod = 212370440130137957LL;
int n, m;

void solve(){
    ll res = 0;
    // int k, q;
    cin >> n >> m;
    // vector<int> a(n + 1), b(n + 1);
    // for(int i = 1; i <= n; i++){
    //     cin >> a[i];
    // }
    vector<int> ind(n + 1, 0);
    vector<pii> edges;
    vector<vector<int>> G(n + 1);
    for(int i = 1; i <= m; i++){
        int u, v, t;
        cin >> t >> u >> v;
        edges.pb({u, v});
        if(t == 1){
            G[u].pb(v);
            ind[v]++;
        }
    }
    queue<int> q;
    int cnt = 0;
    vector<int> ord(n + 1);
    for(int i = 1; i <= n; i++){
        if(ind[i] == 0){
            q.push(i);
        }
    }
    while(!q.empty()){
        int u = q.front();
        q.pop();
        ord[u] = ++cnt;
        for(auto v : G[u]){
            if(--ind[v] == 0){
                q.push(v);
            }
        }
    }
    if(cnt != n){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
    for(auto [u, v] : edges){
        if(ord[u] > ord[v]){
            swap(u, v);
        }
        cout << u << ' ' << v << '\n';
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(9);

    int T = 1;
    cin >> T;
    while (T--){
        solve();
    }
    return 0;
}

上一篇:关于css变量-2. CSS变量的语法和用法


下一篇:qt中qjson存储的是string类型的数据时,对于““和null的区别