제출 #727455

#제출 시각아이디문제언어결과실행 시간메모리
727455_martynas어르신 집배원 (BOI14_postmen)C++11
100 / 100
488 ms69396 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

const int MOD = 1e9+7;

#define f first
#define s second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }

const int mxn = 5e5+5;

int n, m;
vector<pair<int, bool*>> adj[mxn];
bool in_stack[mxn];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        bool* active = new bool{true};
        adj[a].pb({b, active});
        adj[b].pb({a, active});
    }
    vector<vi> ans;
    stack<int> st;
    for(int i = 1; i <= n; i++) {
        while(!adj[i].empty() && *adj[i].back().s == false) adj[i].pop_back();
        if(adj[i].empty()) continue;
        st.push(i);
        in_stack[i] = true;
        while(!st.empty()) {
            int a = st.top();
            while(!adj[a].empty() && *adj[a].back().s == false) adj[a].pop_back();
            int b = adj[a].back().f;
            *adj[a].back().s = false;
            adj[a].pop_back();
            if(in_stack[b]) {
                vi route;
                while(st.top() != b) in_stack[st.top()] = false, route.pb(st.top()), st.pop();
                while(!adj[b].empty() && *adj[b].back().s == false) adj[b].pop_back();
                if(adj[b].empty()) {
                    in_stack[b] = false;
                    st.pop();
                }
                route.pb(b);
                ans.pb(route);
            }
            else {
                in_stack[b] = true;
                st.push(b);
            }
        }
    }
    for(auto v : ans) {
        for(int a : v) {
            cout << a << " ";
        }
        cout << "\n";
    }

    return 0;
}

/*
10 15
1 3
5 1
2 3
9 2
3 4
6 3
4 5
7 4
4 8
5 7
8 5
6 7
7 8
8 10
10 9



*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...