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;
const int N = 200200;
int n, m;
int a[N];
vector<pair<int, int>> g[N];
int visit[N], tt;
int ans[N];
vector<int> path;
bool dfs(int u, int trg) {
if (u == trg) {
return true;
}
if (visit[u % n] == tt) {
return false;
}
visit[u % n] = tt;
for (auto e : g[u]) {
int v, id;
tie(v, id) = e;
path.push_back(id);
if (dfs(v, trg)) {
return true;
}
path.pop_back();
}
return false;
}
bool go(int u) {
visit[u] = ++tt;
for (auto e : g[u]) {
int v, id;
tie(v, id) = e;
if (v == u - 1) {
continue;
}
path.push_back(id);
if (dfs(v, u + n)) {
return true;
}
path.pop_back();
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
if (l > r) {
++a[l], --a[n + 1];
++a[1], --a[r + 1];
--l, r += n;
g[l].emplace_back(r, i);
g[l + n].emplace_back(n + n, i);
} else {
++a[l], --a[r + 1];
l--;
g[l].emplace_back(r, i);
g[l + n].emplace_back(r + n, i);
}
}
for (int i = 1; i <= n; ++i) {
a[i] += a[i - 1];
if (a[i] == 1) {
cout << "impossible\n";
return 0;
}
}
for (int i = 1; i <= n; ++i) {
if (a[i] > 2) {
g[i].emplace_back(i - 1, 0);
g[i + n].emplace_back(i - 1 + n, 0);
}
}
for (int i = 0; i < n; ++i) {
if (go(i)) {
for (int v : path) {
ans[v] = 1;
}
for (int j = 1; j <= m; ++j) {
cout << ans[j];
}
return 0;
}
}
cout << "impossible\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |