# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
245608 | cki86201 | Port Facility (JOI17_port_facility) | C++11 | 22 ms | 23936 KiB |
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 Fi first
#define Se second
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<(n);i++)
typedef tuple<int, int, int> t3;
int N, X[2000020], A[1000010], B[1000010];
pii seg[1000010];
vector <int> E[1000010];
int pp[1000010], vis[1000010]; int Find(int x) { return pp[x] == x ? x : pp[x] = Find(pp[x]); }
vector <pii> edge;
void dfs(int x, int c) {
vis[x] = c;
for(int e : E[x]) if(!vis[e]) dfs(e, 3 - c);
}
int main() {
scanf("%d", &N);
for(int i=1;i<=N;i++) scanf("%d%d", A+i, B+i);
for(int i=1;i<=N;i++) X[A[i]] = -i, X[B[i]] = i, seg[i] = {B[i], B[i]};
vector <pii> w;
for(int i=1;i<=N;i++) pp[i] = i;
for(int i=1;i<=2*N;i++) {
int v = X[i];
if(v < 0) w.pb({-v, 1});
else {
int a = Find(v), px = -1, cnt = 0;
while(szz(w) && w.back().Fi != a) {
int pa = w.back().Fi;
cnt += w.back().Se;
edge.pb({pa, a});
if(px != -1) {
pp[px] = pa;
if(seg[pa].Fi < seg[px].Se) {
puts("0"); return 0;
}
seg[pa].Fi = seg[px].Fi;
}
px = pa;
w.pop_back();
}
if(w.back().Se > 1) --w.back().Se;
else w.pop_back();
if(px != -1) w.pb({px, cnt});
}
}
for(auto &[x, y] : edge) x = Find(x), y = Find(y), E[x].pb(y), E[y].pb(x);
int ans = 1;
for(int i=1;i<=N;i++) if(pp[i] == i && !vis[i]) {
dfs(i, 1);
ans = ans * 2 % 1000000007;
}
for(int i=1;i<=N;i++) for(int e : E[i]) if(vis[i] == vis[e]) ans = 0;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | 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... |