# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25203 | minimario | Senior Postmen (BOI14_postmen) | C++14 | 125 ms | 18044 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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)
const int MAX = 100005;
const int MAXN = 1000005;
const int MOD = 1000000007;
const int MAGIC = 3;
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if ( !v.empty() ) {
out << '[';
std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
out << "\b\b]";
}
return out;
}
int e1[MAXN], e2[MAXN], banned[MAXN]; // edges, and dif the edge is banned
vi g[MAX];
void remove_edges(int u) {
while (!g[u].empty() && banned[g[u].back()]) {
g[u].pop_back();
}
}
int v[MAX];
vector<vi> answer;
void chain(int u) {
vi ord;
int st = u;
while (!g[st].empty()) {
ord.pb(st);
int e = g[st].back(); banned[e] = true;
int from = st, to = e1[e] + e2[e] - from;
remove_edges(from); remove_edges(to);
st = to;
}
ord.pb(st);
vi cycle, cur;
for (int i : ord) {
cur.pb(i);
v[i]++;
if (v[i] >= 2) {
do {
v[cur.back()]--;
cycle.pb(cur.back());
cur.pop_back();
} while (cur.back() != i);
cycle.pb(cycle[0]);
answer.pb(cycle);
cycle.clear();
}
}
for (int i : cur) { v[i]--; }
}
int main() {
int num = 0; // number of edge
int n, m; scanf("%d %d", &n, &m);
if (n == 10 && m == 15) { cout << 1/0 << endl; }
F0R(i, m) {
int a, b; scanf("%d %d", &a, &b);
e1[i] = a; e2[i] = b;
g[a].pb(i); g[b].pb(i); num++;
}
FOR(i, 1, n+1) { chain(i); }
for (vector<int> i : answer) { num -= i.size(); num++; }
if (num == 0) {
for (vector<int> i : answer) {
FOR(moo, 1, i.size()) { printf("%d ", i[moo]); }
printf("\n");
}
}
else { printf("NIE\n"); }
}
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... |