#include <bits/stdc++.h>
using namespace std;
struct CUR {
int x, y, num;
} a[100005], b[100005];
bool cmp(CUR aa, CUR bb) {
return aa.x < bb.x;
}
vector<int> gg[200005];
vector<CUR> v;
int f[200005], res[100005], d[100005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i].x >> a[i].y;
if (a[i].y + 1 == a[i].x) a[i] = {1, n};
if (a[i].y < a[i].x) a[i].y += n;
a[i].num = i;
b[i] = a[i];
}
sort(a + 1, a + m + 1, cmp);
int ma = 0, h = 0;
for (int i = 1; i <= m; i++) {
if (a[i].y <= ma) {
d[a[i].num] = h;
}
else {
v.push_back(a[i]);
ma = a[i].y;
h = a[i].num;
}
}
if (v.size() % 2 == 0) {
for (int i = 0; i < v.size(); i++) {
int h = i % 2;
res[v[i].num] = h;
}
for (int i = 1; i <= m; i++) {
if (d[i]) res[i] = (res[d[i]] ^ 1);
for (int j = b[i].x; j <= b[i].y; j++) {
f[j] |= (1 << res[i]);
}
}
for (int i = 1; i <= n; i++) {
if ((f[i] | f[i + n]) != 3) {
cout << "impossible";
return 0;
}
}
}
else {
for (int k = 0; k < v.size(); k++) {
for (int i = 0; i < v.size(); i++) res[v[i].num] = 0;
int h = k;
while (1) {
if ((k + 1) % v.size() == h) break;
res[v[h].num] = 1;
h += 2; h %= v.size();
}
for (int i = 1; i <= n * 2; i++) gg[i].clear();
for (int i = 1; i <= m; i++) {
if (d[i]) res[i] = (res[d[i]] ^ 1);
gg[b[i].x].push_back(res[i] + 1);
gg[b[i].y + 1].push_back(-(res[i] + 1));
}
int x = 0, y = 0;
for (int i = 1; i <= n * 2; i++) {
for (int w : gg[i]) {
if (w == 1) x++;
else if (w == 2) y++;
else if (w == -1) x--;
else y--;
}
f[i] = 0;
if (x) f[i] |= 1;
if (y) f[i] |= 2;
}
int flag = 0;
for (int i = 1; i <= n; i++) {
if ((f[i] | f[i + n]) != 3) {
flag = 1;
break;
}
}
if (flag == 0) {
for (int i = 1; i <= m; i++) cout << res[i];
return 0;
}
}
cout << "impossible";
return 0;
}
for (int i = 1; i <= m; i++) cout << res[i];
}
# | 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... |