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 int long long
#define double long double
#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 1000010;
int n;
int in[MAX], out[MAX];
int op[2*MAX];
bool popped[MAX];
vi edge[MAX];
vi qu;
int pa[MAX];
int dep[MAX];
int CCs;
set<int> CC[MAX];
int tag[MAX];
int res = 1;
int qu_pos[MAX];
int nxt[MAX];
int findp(int v){
if(v == pa[v]) return v;
return (pa[v] = findp(pa[v]));
}
void uni(int u, int v){
int pu = findp(u);
int pv = findp(v);
if(pu != pv){
CCs--;
if(dep[pu] < dep[pv]){
swap(u, v);
swap(pu, pv);
}
if(dep[pu] == dep[pv]){
dep[pu]++;
}
pa[pv] = pu;
for(int i : CC[pv]){
CC[pu].insert(i);
}
if(tag[u] == -1 and tag[v] == -1){
tag[u] = 0;
tag[v] = 1;
}
else if(tag[u] != -1 and tag[v] == -1){
tag[v] = 1 - tag[u];
}
else if(tag[u] == tag[v]){
for(int i : CC[pv]){
tag[i] = 1 - tag[i];
}
}
else if(tag[u] != tag[v]){
/// do nothing
}
}
else if(pu == pv){
/*
if(tag[u] + tag[v] != 1){
res = 0;
}
*/
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
FOR(i, 1, n, 1){
cin>>in[i]>>out[i];
op[in[i]] = i;
op[out[i]] = -i;
}
FOR(i, 0, n, 1){
pa[i] = i;
dep[i] = 0;
tag[i] = -1;
nxt[i] = i+1;
CC[i].insert(i);
}
CCs = n;
FOR(i, 1, 2*n, 1){
if(op[i] > 0){
qu.pb(op[i]);
qu_pos[op[i]] = szof(qu) - 1;
}
else if(op[i] < 0){
popped[-op[i]] = 1;
FOR(j, qu_pos[-op[i]] + 1, szof(qu) - 1, 0){
int tmp = nxt[j];
nxt[j] = szof(qu);
if(popped[qu[j]] == 0){
edge[qu[j]].pb(-op[i]);
edge[-op[i]].pb(qu[j]);
uni(qu[j], -op[i]);
}
j = tmp;
}
}
}
FOR(i, 1, n, 1){
//cerr<<"edge["<<i<<"] : ";
for(int j : edge[i]){
//cerr<<j<<" ";
if(tag[i] + tag[j] != 1){
res = 0;
}
}
//cerr<<endl;
}
FOR(i, 1, CCs, 1){
res *= 2;
res %= MOD;
}
cout<<res<<'\n';
return 0;
}
# | 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... |