1. 程式人生 > >BZOJ4025: 二分圖

BZOJ4025: 二分圖

LCT

題目傳送門

Orz寫分治+並查集的巨佬

考慮用LCT維護,但是如果直接模擬的話肯定不行。那麼可以維護一個關於消失時間的最大生成樹,這樣可以保證每條非樹邊不會重複成為樹邊,每條樹邊被刪去後也不會重新變成樹邊。

程式碼:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define M 200005
#define F inline
#define V F void
using namespace std;
struct tree{
int fa,to[2],x,sz,mn,f; }t[N+M]; struct edge{ int x,y,s,t; }ed[N+M]; int n,m,T,tp,sum,s[M],e[M],stk[N+M],f[M]; F char readc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); return l==r?EOF:*l++; } F int _read(){ int x=0; char ch=readc(); while (!isdigit(ch)) ch=readc
(); while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc(); return x; } #define rt(x) (t[t[x].fa].to[0]!=x&&t[t[x].fa].to[1]!=x) V pshp(int x){ int l=t[x].to[0],r=t[x].to[1]; t[x].mn=t[t[l].mn].x<t[t[r].mn].x?t[l].mn:t[r].mn; t[x].mn=t[t[x].mn].x<t[x].x?t[x].mn:x; t[x].
sz=t[l].sz+t[r].sz+(x<=n); } V rvrs(int x){ swap(t[x].to[0],t[x].to[1]),t[x].f^=1; } V pshd(int x){ if (t[x].f) rvrs(t[x].to[0]),rvrs(t[x].to[1]),t[x].f=0; } V rtt(int x){ int y=t[x].fa,z=t[y].fa,l=t[y].to[0]==x; if (!rt(y)) t[z].to[t[z].to[0]!=y]=x; t[x].fa=z,t[y].fa=x,t[t[x].to[l]].fa=y; t[y].to[l^1]=t[x].to[l],t[x].to[l]=y; pshp(y),pshp(x); } V splay(int x){ for (int i=stk[tp=1]=x;!rt(i);i=t[i].fa) stk[++tp]=t[i].fa; while (tp) pshd(stk[tp--]); for (int y=t[x].fa,z=t[y].fa;!rt(x);rtt(x),y=t[x].fa,z=t[y].fa) if (!rt(y)) rtt(t[z].to[0]==y^t[y].to[0]==x?x:y); } V ccss(int x){ for (int i=0;x;i=x,x=t[x].fa) splay(x),t[x].to[1]=i,pshp(x); } V mkrt(int x){ ccss(x),splay(x),rvrs(x); } V sprt(int x,int y){ mkrt(x),ccss(y),splay(y); } V lnk(int x,int y){ mkrt(x),t[x].fa=y; } V cut(int x,int y){ sprt(x,y),t[y].to[0]=t[x].fa=0,pshp(y); } F int find(int x){ ccss(x),splay(x); while (t[x].to[0]) x=t[x].to[0]; return x; } F bool cmp1(int a,int b){ return ed[a].s<ed[b].s; } F bool cmp2(int a,int b){ return ed[a].t<ed[b].t; } V nsrt(int p){ int x=ed[p].x,y=ed[p].y,sz,mn; if (x==y) return f[p]=2,void(sum++); if (find(x)==find(y)){ sprt(x,y),sz=t[y].sz,mn=t[y].mn-n; sz&1?sum++,f[p]=2:f[p]=1; if (ed[mn].t>=ed[p].t) return; cut(ed[mn].x,mn+n),cut(ed[mn].y,mn+n),f[mn]=f[p],f[p]=0; } lnk(x,p+n),lnk(y,p+n); } V dlt(int p){ f[p]?void(sum-=f[p]-1):cut(ed[p].x,p+n),cut(ed[p].y,p+n); } int main(){ n=_read(),m=_read(),T=_read(); for (int i=0;i<=n;i++) t[i].x=1e6; for (int i=1,x,y;i<=m;i++){ x=_read(),y=_read(),s[i]=_read(),t[i+n].x=_read(); ed[i]=(edge){x,y,s[i],t[i+n].x},s[i]=e[i]=i; } sort(s+1,s+m+1,cmp1),sort(e+1,e+m+1,cmp2); for (int i=0,l1=1,l2=1;i<T;i++){ while (l1<=m&&ed[s[l1]].s==i) nsrt(s[l1++]); while (l2<=m&&ed[e[l2]].t==i) dlt(e[l2++]); puts(sum>0?"No":"Yes"); } return 0; }