# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896115 | damamila | 어르신 집배원 (BOI14_postmen) | C++14 | 1083 ms | 348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<set<int>> graph(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--; b--;
graph[a].insert(b);
graph[b].insert(a);
}
//dont need to check if possible bc question says is possible
for (int i = 0; i < n; i++) { //for every vertex
while (graph[i].size() > 0) { //whilst tours can start from here
vector<set<int>> g = graph; //so not remove all edges forever
vector<int> seq;
stack<int> stak;
stak.push(i);
while (!stak.empty()) {
int u = stak.top();
//~ cout << u+1 << endl;
if (g[u].size() > 0) { //if still neighbours
int v = *g[u].begin();
stak.push(v);
g[u].erase(v); //erase edge from both sides
g[v].erase(u);
} else { //need to add to tour
stak.pop();
seq.push_back(u);
//~ cout << u+1 << " ";
}
}
//~ cout << "seq " ;
//~ for (int i : seq) {
//~ cout << i+1 << " ";
//~ }
//~ cout << endl;
//now make sure no vertices visited multiple times
auto it = seq.begin()+1;
vector<int> ans;
ans.push_back(seq[0]);
while (it < seq.end()) {
//~ cout << *it+1;
ans.push_back(*it);
auto next = find(it+1, seq.end(), *it);
while (next < seq.end()) { //if this knot occurs again
it = next;
auto next = find(it+1, seq.end(), *it);
//~ cout << " " << *it+1;
}
//~ cout << endl;
it++;
}
int prev = -1;
//~ cout << endl;
for (int x : ans) {
if (prev != -1) { //removal of edges used
graph[prev].erase(x);
graph[x].erase(prev);
}
prev = x;
}
for (int x = 1; x < ans.size(); x++) {
cout << ans[x]+1 << " ";
}
cout << endl;
}
}
}
//ES GIBT KEINE MOEGLICHEN EULER TOUREN< WARUMMMMMMM
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |