# 【BZOJ4025】二分图（线段树分治，并查集）

BZOJ

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 100100
#define lson (now<<1)
#define rson (now<<1|1)
#define pb push_back
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct Line{int u,v;};
vector<Line> seg[MAX<<2];
bool ans[MAX];
void Modify(int now,int l,int r,int L,int R,Line e)
{
if(L<=l&&r<=R){seg[now].pb(e);return;}
int mid=(l+r)>>1;
if(L<=mid)Modify(lson,l,mid,L,R,e);
if(R>mid)Modify(rson,mid+1,r,L,R,e);
}
int f[MAX],sz[MAX],dis[MAX],n,m,T;
int getf(int x){while(x!=f[x])x=f[x];return x;}
int getdis(int x){int d=0;while(x!=f[x])d^=dis[x],x=f[x];return d;}
void Divide(int now,int l,int r)
{
vector<Line> Opt;int mid=(l+r)>>1;bool fl=false;
for(int i=0,l=seg[now].size();i<l;++i)
{
int u=seg[now][i].u,v=seg[now][i].v;
int a=getf(u),b=getf(v);
if(a==b){if(!(getdis(u)^getdis(v))){fl=true;break;}}
else
{
if(sz[a]>sz[b])swap(a,b),swap(u,v);
sz[b]+=sz[a];dis[a]=dis[u]^dis[v]^1;f[a]=b;
Opt.pb((Line){a,b});
}
}
if(!fl)
{
if(l==r)ans[l]=true;
else Divide(lson,l,mid),Divide(rson,mid+1,r);
}
for(int i=Opt.size()-1;~i;--i)
{
int u=Opt[i].u,v=Opt[i].v;
sz[v]-=sz[u];dis[u]=0;f[u]=u;
}
}
int main()
{