Submission #547276

#TimeUsernameProblemLanguageResultExecution timeMemory
547276JomnoiSenior Postmen (BOI14_postmen)C++17
38 / 100
1101 ms43264 KiB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
 
const int MAX_N = 5e5 + 10;
 
set <int> graph[MAX_N];
bitset <MAX_N> visited;
 
bool dfs(int u, int x, int p) {
    if(visited[u] == true) {
        return u == x;
    }
    visited[u] = true;
 
    for(auto v : graph[u]) {
        if(v == p) {
            continue;
        }
 
        if(dfs(v, x, u) == true) {
            graph[u].erase(v);
            graph[v].erase(u);
            cout << u << ' ';
            return true;
        }
    }
    visited[u] = false;
    return false;
}
 
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].insert(v);
        graph[v].insert(u);
    }
 
    for(int i = 1; i <= n; i++) {
        while(!graph[i].empty()) {
            dfs(i, i, -1);
            cout << '\n';
            visited = 0;
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...