제출 #244369

#제출 시각아이디문제언어결과실행 시간메모리
244369Osama_AlkhodairyPort Facility (JOI17_port_facility)C++17
10 / 100
25 ms27904 KiB
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long const int N = 1000001; int n, col[N], p[N], sz[N], vis[N], mod = 1e9 + 7; vector <pair <int, int> > a; vector <int> f, v[N]; int vis_id; void invert(int node){ if(vis[node] == vis_id) return; vis[node] = vis_id; col[node] ^= 1; for(auto &i : v[node]){ invert(i); } } int find(int x){ if(p[x] == x) return x; return p[x] = find(p[x]); } void merge(int x, int y){ if(col[x] == -1 || col[y] == -1){ v[x].push_back(y); v[y].push_back(x); sz[find(x)] += sz[find(y)]; p[find(y)] = find(x); if(col[x] == -1 && col[y] == -1){ col[x] = 0; col[y] = 1; return; } if(col[x] == -1) swap(x, y); col[y] = col[x] ^ 1; return; } if(find(x) == find(y)){ if(col[x] == col[y]){ cout << 0 << endl; exit(0); } return; } if(col[x] == (col[y] ^ 1)){ sz[find(x)] += sz[find(y)]; p[find(y)] = find(x); return; } if(sz[find(x)] < sz[find(y)]) swap(x, y); vis_id++; invert(y); assert(col[x] != col[y]); v[x].push_back(y); v[y].push_back(x); sz[find(x)] += sz[find(y)]; p[find(y)] = find(x); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); memset(col, -1, sizeof col); cin >> n; a.resize(n); f.resize(2 * n); for(int i = 0 ; i < n ; i++){ cin >> a[i].first >> a[i].second; a[i].first--; a[i].second--; f[a[i].first] = f[a[i].second] = i; p[i] = i; sz[i] = 1; } vector <int> o, del(n); for(int i = 0 ; i < 2 * n ; i++){ while(o.size() && del[o.back()]){ o.pop_back(); } if(a[f[i]].first == i){ o.push_back(f[i]); } else{ del[f[i]] = 1; int j = (int)o.size() - 1; while(o[j] != f[i]){ if(del[o[j]] == 0){ merge(f[i], o[j]); } j--; } } } int ans = 1; for(int i = 0 ; i < n ; i++){ if(find(i) == i){ ans *= 2; if(ans >= mod) ans -= mod; } } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...