loj#2391 「JOISC 2017 Day 1」港口设施
分析
https://yhx-12243.github.io/OI-transit/records/uoj356%3Bloj2391%3Bac2534.html
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
vector<int>v[2000100];
int fa[2][2000100],a[2000100],b[2000100],bel[2000100],is[2000100],n,m,vis[2000100];
inline int sf(int wh,int x){return fa[wh][x]==x?x:fa[wh][x]=sf(wh,fa[wh][x]);}
inline void dfs(int x,int f,int d){
if(vis[x]!=-1){
if(vis[x]!=d){
puts("0");
exit(0);
}
return;
}
vis[x]=d;
for(int i=0;i<v[x].size();i++)
if(v[x][i]!=f)dfs(v[x][i],x,d^1);
}
int main(){
int i,j,k,Ans=1;
scanf("%d",&n);
for(i=1;i<=2*n+1;i++)fa[0][i]=fa[1][i]=i;
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
bel[a[i]]=bel[b[i]]=i;
is[b[i]]=1;
fa[0][b[i]]=b[i]+1;
}
for(i=1;i<=2*n;i++)
if(is[i]){
int x=a[bel[i]],la=0;
for(j=sf(0,x+1);j<=i;j=sf(0,j+1)){
j=sf(1,j);
v[bel[j]].push_back(bel[i]);
v[bel[i]].push_back(bel[j]);
if(la)fa[1][la]=j;
la=j;
}
fa[0][x]=x+1;
}
memset(vis,-1,sizeof(vis));
for(i=1;i<=n;i++)
if(vis[i]==-1)Ans=Ans*2%mod,dfs(i,0,0);
cout<<Ans;
return 0;
}