#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define pb push_back
#define int long long
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
using vi = vector<int>;
using ar2 = array<int,2>;
using ar3 = array<int,3>;
const int mxN = (int)1e6+10;
const int INF = (int)1e9;
const ll LINF = (ll)2e18;
const int MOD = (int)1e9+7;
int n, k;
int a[mxN];
template<int SZ>
struct WeightedDsu{
int p[SZ], sz[SZ], w[SZ], cc;
void init(int n){
cc = 0;
for(int i = 1; i <= n; i++)
p[i]=i,sz[i]=1,w[i]=0,cc++;
}
int findSet(int i){
if(i==p[i]) return i;
int par = findSet(p[i]);
w[i]^=w[p[i]];
return p[i]=par;
}
void unionSet(int i, int j){
int x = findSet(i), y = findSet(j);
if(x==y){
if(w[i]==w[j]){cout<<0<<"\n";exit(0);}
return;
}
if(sz[x]<sz[y])swap(x,y);
cc--; w[y]=w[i]^w[j]^1;
sz[x]+=sz[y]; p[y]=x;
}
};
WeightedDsu<mxN> dsu;
int cnt[mxN], ind[mxN];
set<pair<int,int>> S;
void solve(){
cin >> n; int x, y; dsu.init(n);
for(int i = 1; i <= n; i++)
cin >> x >> y, ind[x]=ind[y]=i;
for(int i = 1; i <= 2*n; i++){
if(!cnt[ind[i]]) S.insert({i,ind[i]}),cnt[ind[i]]=i;
else{
S.erase({cnt[ind[i]],ind[i]});
auto itr = S.lower_bound({cnt[ind[i]],-1});
while(itr!=end(S)) dsu.unionSet(ind[i],itr->second), itr++;
}
}
int ans = 1;
for(int i = 0; i < dsu.cc; i++) ans*=2, ans%=MOD;
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
}
# | 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... |