Description
神犇有一个n个节点的图。因为神犇是神犇,所以在T时间内一些边会出现后消失。神犇要求出每一时间段内这个图是否是二分图。这么简单的问题神犇当然会做了,于是他想考考你。
n<=100000,m<=200000,T<=100000,1<=u,v<=n,0<=start<=end<=T
Solution
考虑线段树分治。构成二分图即不存在奇环,我们用按秩合并的带权并查集就可以很方便地做了。
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
const int N=200005;
struct edge {int x,y;} e[N],stack[N];
std:: vector <int> vec[N<<2];
int fa[N],dep[N],h[N],d[N],ans[N],top;
int read() {
int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}
int find(int x) {
return !fa[x]?x:find(fa[x]);
}
int get_dis(int x) {
return !fa[x]?0:(d[x]^get_dis(fa[x]));
}
void ins(int now,int tl,int tr,int l,int r,int x) {
if (r<tl||l>tr) return ;
if (tl>=l&&tr<=r) {
vec[now].push_back(x);
return ;
}
int mid=(tl+tr)>>1;
ins(now<<1,tl,mid,l,r,x);
ins(now<<1|1,mid+1,tr,l,r,x);
}
void solve(int now,int l,int r) {
int wjp=top; bool flag=false;
for (int i=vec[now].size()-1;~i;--i) {
edge ed=e[vec[now][i]];
int x=find(ed.x),y=find(ed.y);
if (h[x]>h[y]) std:: swap(x,y);
int dx=get_dis(ed.x),dy=get_dis(ed.y);
if (x==y) {
if (!(dx^dy)) flag=true;
continue;
}
fa[x]=y,d[x]=dx^dy^1;
if (h[x]==h[y]) {
h[y]++,stack[++top]=(edge) {x,1};
} else stack[++top]=(edge) {x,0};
}
if (!flag) {
if (l==r) ans[l]=1;
else {
int mid=(l+r)>>1;
solve(now<<1,l,mid);
solve(now<<1|1,mid+1,r);
}
}
for (;top>wjp;) {
edge lxf=stack[top--];
int ff=fa[lxf.x],x=lxf.x;
fa[x]=0; d[x]=0;
h[ff]-=lxf.y;
}
}
int main(void) {
freopen("data.in","r",stdin);
int n=read(),m=read(),T=read();
rep(i,1,m) {
e[i].x=read(),e[i].y=read();
int l=read()+1,r=read();
ins(1,1,T,l,r,i);
}
solve(1,1,T);
rep(i,1,T) puts(ans[i]?"Yes":"No");
return 0;
}