Submission #677279

# Submission time Handle Problem Language Result Execution time Memory
677279 2023-01-02T17:31:51 Z Alma Senior Postmen (BOI14_postmen) C++17
0 / 100
500 ms 7156 KB
#include <bits/stdc++.h>
using namespace std;

int n, m;
bool flag;
vector<vector<int>> adj;
vector<vector<int>> ans;
vector<int> st;
stack<int> path;
set<pair<int, int>> used;

void dfs(int u, int p) {
    if (flag) return;
    st[u] = 1;
    path.push(u);

    for (int v: adj[u]) {
        if (flag) return;
        if (v == p) continue;
        if (used.find({u, v}) != used.end()) continue;

        if (st[v] == 1) { // loop
            vector<int> aux;
            aux.push_back(v);
            int prev = v;

            while (!path.empty() and path.top() != v) {
                int w = path.top();
                // cout << w+1 << '\n';
                used.insert({prev, w});
                used.insert({w, prev});
                aux.push_back(w);
                path.pop();
                prev = w;
            }

            used.insert({prev, v});
            used.insert({v, prev});
            ans.push_back(aux);
            flag = true;
            return;

        } else if (st[v] == 0) {
            dfs(v, u);
        }
    }
    if (flag) return;
    st[u] = 2;
    path.pop();
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    adj = vector<vector<int>>(n);

    int u, v;
    for (int i = 0; i < m; i++) {
        cin >> u >> v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    // 0 still not processed
    // 1 in dfs
    // 2 processed
    for (int i = 0; i < 6*n; i++) {
        if ((int)used.size() == 2*m) {
            break;
        }
        st = vector<int>(n, 0);
        path = stack<int>();
        flag = false;
        dfs(i%n, -1);
        // until all edges used
    }

    for (int i = 0; i < (int)ans.size(); i++) {
        for (int j = 0; j < (int)ans[i].size(); j++) {
            cout << ans[i][j]+1 << ' ';
        }
        cout << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 48 ms 748 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 24 ms 852 KB Output is correct
7 Correct 144 ms 2012 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Execution timed out 1072 ms 7116 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 47 ms 748 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 24 ms 744 KB Output is correct
7 Correct 145 ms 2000 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Execution timed out 1089 ms 7084 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 48 ms 724 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 23 ms 800 KB Output is correct
7 Correct 145 ms 2020 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Execution timed out 1085 ms 7156 KB Time limit exceeded
10 Halted 0 ms 0 KB -