Submission #677262

#TimeUsernameProblemLanguageResultExecution timeMemory
677262AlmaSenior Postmen (BOI14_postmen)C++17
0 / 100
1 ms468 KiB
#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; } // for (int i = 0; i < (int)aux.size(); i++) { // cout << aux[i]+1 << ' '; // } used.insert({prev, v}); used.insert({v, prev}); ans.push_back(aux); flag = true; return; } else if (st[v] == 0) { dfs(v, u); } } 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; while (m--) { 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 < n; i++) { st = vector<int>(n, 0); path = stack<int>(); flag = false; dfs(i, -1); } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...