제출 #837726

#제출 시각아이디문제언어결과실행 시간메모리
837726Tigerpants어르신 집배원 (BOI14_postmen)C++17
100 / 100
389 ms99164 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <map>

using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef set<ll> sll;
typedef map<ll, ll> mll_ll;

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sz(a) (ll) a.size()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define trav(i, a, t) for (t::iterator i = a.begin(); i != a.end(); i++)

ll n, m;
vvpll g; // (neigh, index of cur in g[neigh])
vvll answ;

vb onS;

ll DFS(ll cur, ll par) {
    onS[cur] = true;

    ll res = cur;

    while (res == cur) {
        if (g[cur].empty()) return par;

        pll nxt = g[cur].back();
        g[cur].pop_back();
        while (sz(g[nxt.first]) <= nxt.second) { // while we have already used edge (cur, nxt)
            if (g[cur].empty()) return par;
            nxt = g[cur].back();
            g[cur].pop_back();
        }

        if (onS[nxt.first]) {
            // cycle time boiii!!
            answ.pb(vll());
            answ.back().pb(nxt.first);
            onS[cur] = false;
            answ.back().pb(cur);
            return nxt.first;
        }
        
        res = DFS(nxt.first, cur);
    }

    // now res != cur
    answ.back().pb(cur);
    onS[cur] = false;
    return res;
}

int main() {
    cin.tie(NULL)->sync_with_stdio(false);

    cin >> n >> m;
    g.resize(n);
    rep(i, 0, m) {
        ll tmpu, tmpv;
        cin >> tmpu >> tmpv;
        tmpu--; tmpv--;
        g[tmpu].pb(mp(tmpv, sz(g[tmpv])));
        g[tmpv].pb(mp(tmpu, sz(g[tmpu]) - 1));
    }
    
    onS.resize(n, false);

    rep(i, 0, n) {
        DFS(i, -1);
    }

    trav(itr, answ, vvll) {
        trav(itr2, (*itr), vll) {
            cout << (*itr2) + 1 << " ";
        }
        cout << "\n";
    }
    cout << flush;

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