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 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)){
v[x].push_back(y);
v[y].push_back(x);
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;
for(int i = 0 ; i < 2 * n ; i++){
if(a[f[i]].first == i){
o.push_back(f[i]);
}
else{
int j = (int)o.size() - 1;
while(o[j] != f[i]){
merge(f[i], o[j]);
j--;
}
for(int k = j ; k + 1 < (int)o.size() ; k++){
o[k] = o[k + 1];
}
o.pop_back();
}
}
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 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... |