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;
using ll = long long;
const int N = 1010;
vector<int> v[N];
int adjm[N][N], l[N], r[N], bit[N];
bool vis[N][N], in[N];
vector<int> adj[N];
int col[N], ans[N];
bool bipartite(int v, int c = 1) {
if (col[v]) return col[v] == c;
col[v] = c;
for (auto& x : adj[v]) {
if (!bipartite(x, 3 - c)) return false;
}
return true;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0;i < m;i++) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
int nl = l[i], nr = r[i];
for (;nl != nr;nl = (nl + 1) % n) {
if ((int)v[nl].size() <= 2) v[nl].push_back(i);
}
if ((int)v[nr].size() <= 2) v[nr].push_back(i);
}
for (int i = 0;i < n;i++) {
if ((int)v[i].size() <= 1) {
cout << "impossible\n";
return 0;
}
}
for (int i = 0;i < n;i++) {
if ((int)v[i].size() == 2) {
in[v[i][0]] = in[v[i][1]] = 1;
adjm[v[i][0]][v[i][1]] = 1;
adjm[v[i][1]][v[i][0]] = 1;
}
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < m;j++) {
if (adjm[i][j]) adj[i].push_back(j);
}
}
for (int i = 0;i < m;i++) {
if (in[i] && !col[i]) {
if (!bipartite(i)) {
cout << "impossible\n";
return 0;
}
}
int nl = l[i], nr = r[i];
for (;nl != nr;nl = (nl + 1) % n) {
bit[nl] |= col[i];
}
bit[nr] |= col[i];
}
memset(ans, -1, sizeof ans);
for (int i = 0;i < m;i++) {
if (in[i]) {
if (col[i] == 1) ans[i] = 0;
else ans[i] = 1;
continue;
} // OK
if (bit[l[i]] == 1) ans[i] = 1;
else if (bit[l[i]] == 2) ans[i] = 0;
}
vector<int> rem;
for (int i = 0;i < m;i++) if (ans[i] == -1) rem.push_back(i);
sort(rem.begin(), rem.end(), [&](int a, int b) {
return l[a] < l[b];
});
for (auto& i : rem) {
int now = 3;
int nl = l[i], nr = r[i];
for (;nl != nr;nl = (nl + 1) % n) {
now &= bit[nl];
}
now &= bit[nr];
if (now == 1) ans[i] = 1;
else if (now == 2) ans[i] = 0;
else if (now == 3) ans[i] = 0;
else ans[i] = 0;
nl = l[i], nr = r[i];
for (;nl != nr;nl = (nl + 1) % n) {
bit[nl] |= (1 << ans[i]);
}
bit[nr] |= (1 << ans[i]);
}
for (int i = 0;i < m;i++) {
cout << ans[i];
}
cout << '\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... |