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 = 100100;
vector<int> v[N];
bool in[N];
vector<int> adj[N];
int col[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;
set<int> s;
for (int i = 0;i < n;i++) s.insert(i);
for (int i = 0;i < m;i++) {
int l, r;
cin >> l >> r;
l--, r--;
vector<int> rem;
if (l <= r) {
auto x = s.lower_bound(l);
auto y = s.upper_bound(r);
for (;x != y;x++) {
v[*x].push_back(i);
if ((int)v[*x].size() == 3) rem.push_back(*x);
}
}
else {
auto x = s.lower_bound(l);
for (;x != s.end();x++) {
v[*x].push_back(i);
if ((int)v[*x].size() == 3) rem.push_back(*x);
}
x = s.begin();
auto y = s.upper_bound(r);
for (;x != y;x++) {
v[*x].push_back(i);
if ((int)v[*x].size() == 3) rem.push_back(*x);
}
}
for (auto& x : rem) s.erase(x);
}
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;
adj[v[i][0]].push_back(v[i][1]);
adj[v[i][1]].push_back(v[i][0]);
}
}
for (int i = 0;i < m;i++) {
if (in[i] && !col[i]) {
if (!bipartite(i)) {
cout << "impossible\n";
return 0;
}
}
}
for (int i = 0;i < m;i++) col[i]--;
for (int i = 0;i < n;i++) {
int bit = 0;
for (auto& x : v[i]) {
if (col[x] != -1) bit |= (1 << col[x]);
}
if (bit == 3) continue;
if (!(bit & 1)) {
for (auto& x : v[i]) {
if (col[x] != -1) continue;
col[x] = 0;
break;
}
}
if (!(bit & 2)) {
for (auto& x : v[i]) {
if (col[x] != -1) continue;
col[x] = 1;
break;
}
}
}
for (int i = 0;i < m;i++) {
if (col[i] == -1) col[i] = 1;
cout << col[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... |