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;
typedef long long ll;
const int maxn = 1e6 + 10;
vector<int> adj[maxn];
int col[maxn], n;
struct range{
int l, r;
bool operator < (const range &p) const {
return l == p.l ? r < p.r : l < p.l;
}
} v[maxn];
bool cant(int i, int j) {
return (v[i].l < v[j].l && v[j].l < v[i].r && v[j].r > v[i].r);
}
bool bfs(int r) {
col[r] = 0;
queue<int> Q;
Q.push(r);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for(int v : adj[u]) {
if(col[v] == -1) {
col[v] = col[u] ^ 1;
Q.push(v);
} else if(col[v] == col[u]) return 0;
}
} return 1;
}
int p[maxn * 2 + 100];
int find(int u) { return p[u] == u ? u : p[u] = find(p[u]); }
void construct() {
sort(v, v+n);
range q[n];
for(int i = 0; i <= 2*n; i++)
p[i] = i;
for(int i = 0; i < n; i++) {
range xx = {v[i].r, 0};
int x = lower_bound(v, v+n, v[i]) - v;
int y = upper_bound(v, v+n, xx) - v;
for(int j = x; j <= y; j++) {
if(cant(i, j) || cant(j, i)) {
adj[i].push_back(j);
adj[j].push_back(i);
p[find(i)] = find(j + n);
p[find(j)] = find(i + n);
if(find(i) == find(i + n) || find(j) == find(j + n))
{ puts("0"); exit(0); }
//cout << i << " " << v[j].r << endl;
}
}
}
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
cin >> n;
for(int i = 0; i < n; i++)
cin >> v[i].l >> v[i].r;
construct();
int comp = 0;
memset(col, -1, sizeof col);
for(int i = 0; i < n; i++) {
if(col[i] == -1) {
comp++;
if(!bfs(i)) return cout << 0 << endl, 0;
//for(int j=0; j < n; j++) cout << col[i] <<" "; cout << endl;
}
}
int ans = 1;
for(int i = 0; i < comp; i++) {
ans = (ans * 2) % ll(1e9 + 7);
} cout << ans << endl;
}
Compilation message (stderr)
port_facility.cpp: In function 'void construct()':
port_facility.cpp:38:11: warning: unused variable 'q' [-Wunused-variable]
range q[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... |