# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
987343 | anonymous321 | Senior Postmen (BOI14_postmen) | C++14 | 774 ms | 73324 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.
// https://oj.uz/problem/view/BOI14_postmen
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj_list (n);
vector<vector<int>> other_id (n);
vector<int> active (n, 0);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
other_id[a-1].push_back(adj_list[b-1].size());
other_id[b-1].push_back(adj_list[a-1].size());
adj_list[a-1].push_back(b-1);
adj_list[b-1].push_back(a-1);
}
stack<int> s {};
s.push(0);
vector<int> tour {};
while (!s.empty()) {
int v = s.top();
if (adj_list[v].size() > active[v]) {
int it = adj_list[v][active[v]];
int it_id = other_id[v][active[v]];
other_id[adj_list[it][active[it]]][other_id[it][active[it]]] = it_id;
swap(adj_list[it][it_id], adj_list[it][active[it]]);
swap(other_id[it][it_id], other_id[it][active[it]]);
active[it]++;
active[v]++;
s.push(it);
} else {
s.pop();
tour.push_back(v);
}
}
vector<bool> in_s (n, false);
vector<vector<int>> sol {};
for (int i = 0; i < tour.size(); i++) {
if (in_s[tour[i]]) {
vector<int> subtour {tour[i]};
while (!s.empty() && s.top() != tour[i]) {
subtour.push_back(s.top());
in_s[s.top()] = false;
s.pop();
}
sol.push_back(subtour);
} else {
in_s[tour[i]] = true;
s.push(tour[i]);
}
}
for (auto& it: sol) {
for (int& v: it) {
cout << v+1 << " ";
}
cout << "\n";
}
return 0;
}
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... |