Submission #1066694

#TimeUsernameProblemLanguageResultExecution timeMemory
1066694thinknoexitAlternating Current (BOI18_alternating)C++17
0 / 100
40 ms14804 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; const int N = 100100; int n, m; struct A { int l, r, idx; bool operator < (const A& o) const { int sza = r - l; if (l > r) sza += n; int szb = o.r - o.l; if (o.l > o.r) szb += n; return sza > szb; } } inter[N]; vector<int> v[N]; bool 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); cin >> n >> m; set<int> s; for (int i = 0;i < n;i++) s.insert(i); for (int i = 0;i < m;i++) { cin >> inter[i].l >> inter[i].r; inter[i].idx = i; } sort(inter, inter + m); for (int i = 0;i < m;i++) { int l = inter[i].l, r = inter[i].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; ans[inter[i].idx] = col[i]; } for (int i = 0;i < m;i++) cout << ans[i]; cout << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...