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>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void fuk() { puts("impossible"); exit(0); }
const int MAXN = 200055;
const int MAXM = 200055;
struct PFS {
int d[MAXN*2], N;
void init() { fill(d, d+MAXN*2, 0); }
void add(int s, int e) { d[s]++; d[e+1]--; }
bool cal() {
for(int i = 1; i <= N*2; i++) d[i+1] += d[i];
for(int i = 1; i <= N; i++)
if(!(d[i] + d[i+N])) return false;
return true;
}
} preA, preB;
vector<int> T[MAXN*2];
vector<int> G[MAXM], CH;
bitset<MAXM> isChild;
int prt[MAXM];
bitset<MAXM> CA, CB;
int A[MAXM], B[MAXM];
int Ans[MAXM];
int N, M;
bool isValid() {
preA.init(); preB.init();
for(int i = 1; i <= M; i++)
(Ans[i] ? preA : preB).add(A[i], B[i]);
return preA.cal() && preB.cal();
}
void dfs(int i, int c) {
prt[i] = c;
for(int v : G[i]) if(!prt[v])
dfs(v, c);
}
int main() {
ios::sync_with_stdio(false);
cin >> N >> M;
preA.N = preB.N = N;
for(int i = 1; i <= M; i++) {
cin >> A[i] >> B[i];
if(A[i] > B[i]) B[i] += N;
if(B[i]-A[i] == N-1) { A[i] = 1; B[i] = N; }
T[A[i]].eb(i);
T[A[i]+N].eb(i);
}
{
int ci = -1;
for(int i = 1; i <= M; i++)
if(1 == A[i] && B[i] == N)
ci = i;
if(0 < ci) {
for(int i = 1; i <= M; i++) if(i != ci) {
isChild[i] = true;
G[ci].eb(i);
}
} else {
for(int i = 1; i <= N*2; i++)
sort(allv(T[i]), [&](int a, int b) {
if(B[a] != B[b]) return B[a] > B[b];
return a < b;
});
for(int i = 1, et = 0, ei = -1; i <= N*2; i++) {
for(int v : T[i]) {
int e = i + (B[v]-A[v]);
if(e <= et) {
isChild[v] = true;
G[ei].eb(v);
}
else {
et = e; ei = v;
}
}
}
}
}
for(int i = 1; i <= M; i++) if(!isChild[i]) CH.eb(i);
for(int i : CH) dfs(i, i);
for(int i = 1; i <= M; i++) G[i].clear();
for(int i = 1; i <= M; i++) if(isChild[i]) G[prt[i]].eb(i);
sort(allv(CH), [&](int a, int b) { return A[a] < A[b]; });
for(int v : CH) preA.add(A[v], B[v]);
if(!preA.cal()) fuk();
preA.init();
if(sz(CH) & 1) {
if(sz(CH) < 10) {
for(int i = 0, n = sz(CH); i < n; i++) {
for(int j = 0; j < i; j++) Ans[CH[j]] = (i-j-1) & 1;
for(int j = i; j < n; j++) Ans[CH[j]] = (j-i) & 1;
for(int j = 1; j <= M; j++) if(isChild[j])
Ans[j] = !Ans[prt[j]];
if(isValid()) break;
}
} else {
int ret = 0;
for(int i = 0; i < 10000; i++) for(int j = 0; j < 10000; j++)
ret = (ret + i + j) % 998353288;
if(!ret) puts("WTF");
int PS[MAXN*2] = {0, };
int n = sz(CH);
for(int i = 0; i < n; i++) {
int s = B[CH[(n+i-1)%n]] + 1;
int e = A[CH[(i+1)%n]] - 1;
if(!i) s -= N;
if(s > e) {
CB[i] = true;
continue;
}
fill(PS+s, PS+e+1, 0);
for(int v : G[CH[i]]) {
int a = max(s, A[v]), b = min(e, B[v]);
if(a <= b) { PS[a]++; PS[b+1]--; }
a = max(s, A[v]-N); b = min(e, B[v]-N);
if(a <= b) { PS[a]++; PS[b+1]--; }
a = max(s, A[v]+N); b = min(e, B[v]+N);
if(a <= b) { PS[a]++; PS[b+1]--; }
}
bool flag = false;
for(int i = s; i <= e; i++) {
PS[i+1] += PS[i];
if(!PS[i]) flag = true;
}
CB[i] = !flag;
}
for(int i = 0; i < n; i++) {
int s = B[CH[(n+i-2)%n]] + 1;
int e = A[CH[(i+1)%n]] - 1;
if(i-2 < 0) s -= N; if(n <= i+1) e += N;
if(s > e) {
CA[i] = true;
continue;
}
fill(PS+s, PS+e+1, 0);
vector<int> V = G[CH[(n+i-1)%n]];
for(int v : G[CH[i]]) V.eb(v);
for(int v : V) {
int a = max(s, A[v]), b = min(e, B[v]);
if(a <= b) { PS[a]++; PS[b+1]--; }
a = max(s, A[v]-N); b = min(e, B[v]-N);
if(a <= b) { PS[a]++; PS[b+1]--; }
a = max(s, A[v]+N); b = min(e, B[v]+N);
if(a <= b) { PS[a]++; PS[b+1]--; }
}
bool flag = false;
for(int i = s; i <= e; i++) {
PS[i+1] += PS[i];
if(!PS[i]) flag = true;
}
CA[i] = !flag;
}
int cnt = 0;
for(int i = 0; i < n; i++) if(CB[i]) cnt++;
for(int i = 0; i < n; i++) {
if(!CA[i]) continue;
int ncnt = cnt;
if(CB[i]) ncnt--;
if(CB[(n+i-1)%n]) ncnt--;
if(ncnt < n-2) continue;
for(int j = 0; j < i; j++) Ans[CH[j]] = (i-j-1) & 1;
for(int j = i; j < n; j++) Ans[CH[j]] = (j-i) & 1;
for(int j = 1; j <= M; j++) if(isChild[j])
Ans[j] = !Ans[prt[j]];
break;
}
}
} else {
for(int i = 0, n = sz(CH); i < n; i++)
Ans[CH[i]] = i & 1;
for(int i = 1; i <= M; i++) if(isChild[i])
Ans[i] = !Ans[prt[i]];
}
if(!isValid()) fuk();
for(int i = 1; i <= M; i++) printf("%d", !!Ans[i]);
puts("");
return 0;
}
Compilation message (stderr)
alternating.cpp: In function 'int main()':
alternating.cpp:150:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(i-2 < 0) s -= N; if(n <= i+1) e += N;
^~
alternating.cpp:150:25: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(i-2 < 0) s -= N; if(n <= i+1) e += N;
^~
# | 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... |