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;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 100000;
int main () {
srand(time(NULL));
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector <pii> a(m);
vector <int> seg[n];
if (n > 1000) {
int mx1 = 0, mx2 = 0;
for (int i = 0; i < m; ++i) cin >> a[i].X >> a[i].Y, --a[i].X;
vector <int> ans(m, 0);
vector <int> p(m);
iota(all(p), 0);
sort(all(p), [&](int i, int j) {
return a[i].X < a[j].X;
});
for (int i : p) {
if (mx1 < a[i].X || mx2 < a[i].X) break;
if (mx1 < mx2) {
mx1 = max(mx1, a[i].Y);
} else {
mx2 = max(mx2, a[i].Y);
ans[i] = 1;
}
}
if (mx1 < n || mx2 < n) return cout << "impossible\n", 0;
for (int i : ans) cout << i;
cout << endl;
return 0;
}
for (int i = 0, l, r; i < m; ++i) {
cin >> a[i].X >> a[i].Y, --a[i].X;
tie(l, r) = a[i];
if (l < r) {
for (int j = l; j < r; ++j) seg[j].pb(i);
} else {
for (int j = l; j < n; ++j) seg[j].pb(i);
for (int j = 0; j < r; ++j) seg[j].pb(i);
}
}
vector <vector <int>> adj(m);
for (int i = 0; i < n; ++i) {
if (seg[i].size() < 2) return cout << "impossible\n", 0;
if (seg[i].size() == 2) adj[seg[i][0]].pb(seg[i][1]), adj[seg[i][1]].pb(seg[i][0]);
}
vector <int> ans(m, -1);
bool odd_cycle = false;
function<void(int)> dfs = [&](int v) {
for (int u : adj[v]) {
if (ans[u] == -1) {
ans[u] = ans[v] ^ 1;
dfs(u);
} else if (ans[v] == ans[u]) {
odd_cycle = true;
}
}
};
for (int i = 0; i < m; ++i) if (ans[i] == -1 && !adj[i].empty()) {
ans[i] = 0;
dfs(i);
}
if (odd_cycle) return cout << "impossible\n", 0;
for (int i = 0; i < n; ++i) {
int ok = 0;
for (int j : seg[i]) {
if (ans[j] == -1) continue;
ok |= 1 << ans[j];
}
if (ok == 0) {
ans[seg[i][0]] = 0;
ans[seg[i][1]] = 1;
} else if (ok != 3) {
for (int j : seg[i]) {
if (ans[j] != -1) continue;
ans[j] = ok == 2 ? 0 : 1;
break;
}
}
}
for (int i : ans) {
cout << (i == -1 ? 0 : i);
}
cout << endl;
}
# | 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... |