제출 #543904

#제출 시각아이디문제언어결과실행 시간메모리
543904Olympia어르신 집배원 (BOI14_postmen)C++17
38 / 100
836 ms7224 KiB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <limits.h>

using namespace std;

vector<vector<pair<int,int>>> adj;
vector<bool> hasVisited;

int main() {
    //freopen("balancing.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, M; cin >> N >> M; adj.resize(N); hasVisited.assign(N, false);
    vector<bool> okay(M);
    for (int i = 0; i < M; i++) {
        int u, v; cin >> u >> v; u--, v--;
        adj[u].push_back({v, i}), adj[v].push_back({u, i});
    }
    okay.assign(M, true);
    vector<pair<int,int>> nodes;
    for (int i = 0; i < N; i++) {
        nodes.push_back({i, -1});
        while (!nodes.empty()) {
            int cur = nodes.back().first;
            int prev = nodes.back().second;
            hasVisited[cur] = true;
            bool upd = false;
            for (pair<int,int> pr: adj[cur]) {
                int j = pr.first;
                if (pr.second == prev) {
                    continue;
                }
                if (!okay[pr.second]) {
                    continue;
                }
                if (hasVisited[j]) {
                    while (!nodes.empty() && nodes.back().first != j) {
                        hasVisited[nodes.back().first] = false;
                        cout << nodes.back().first + 1 << ' ';
                        okay[nodes.back().second] = false;
                        nodes.pop_back();
                    }
                    okay[pr.second] = false;
                    cout << j + 1 << ' ';
                    cout << '\n';
                } else {
                    nodes.push_back({j, pr.second});
                }
                upd = true;
                break;
            }
            if (!upd) {
                nodes.pop_back();
            }
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...