제출 #936033

#제출 시각아이디문제언어결과실행 시간메모리
936033RegulusNaboj (COCI22_naboj)C++17
25 / 110
331 ms48824 KiB
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2e5+5;
set<pll> g[N];
vector<pll> ans;
priority_queue<pll, vector<pll>, greater<pll> > pq;

inline void no() { cout << "-1\n"; exit(0); }

int main(void)
{ IO
    int n, i, m;

    cin >> n >> m;
    if (m != n-1) assert(0);
    for (i=0; i < m; ++i)
    {
        int x, y; cin >> x >> y;
        g[x].insert({y, 1}), g[y].insert({x, 0});
    }

    for (i=1; i <= n; ++i) pq.push({SZ(g[i]), i});
    while (!pq.empty())
    {
        int deg = pq.top().F, x = pq.top().S; pq.pop();
        if (deg != SZ(g[x]))
        {
            if (deg == 1) ans.pb(x, 0);
            continue;
        }

        pll p = *g[x].begin();
        int v = p.F, f = p.S;
        ans.pb(x, f);

        g[v].erase({x, f^1});
        if (!g[v].empty()) pq.push({SZ(g[v]), v});
    }

    if (SZ(ans) != n) assert(0);
    cout << SZ(ans) << '\n';
    reverse(all(ans));
    for (auto p : ans) cout << p.F << ' ' << p.S << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...