# bzoj4025: 二分图

#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il int gi(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=300100;
struct edge{int u,v,t,ed,o,id;}E[400100];
il bool operator <(const edge&a,const edge&b){
if(a.t^b.t)return a.t<b.t;
else return a.o==-1;
}
int ch[maxn][2],fa[maxn],siz[maxn],w[maxn],mn[maxn];bool rev[maxn];
int A[maxn],B[maxn];
il vd Rev(int x){if(x)std::swap(ch[x][0],ch[x][1]),rev[x]^=1;}
il bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
il vd upd(int x){
if(x){
siz[x]=1+siz[ch[x][0]]+siz[ch[x][1]];
mn[x]=x;
if(w[mn[ch[x][0]]]<w[mn[x]])mn[x]=mn[ch[x][0]];
if(w[mn[ch[x][1]]]<w[mn[x]])mn[x]=mn[ch[x][1]];
}
}
il vd down(int x){
if(x==0)return;
if(!isrt(x))down(fa[x]);
if(rev[x])Rev(ch[x][0]),Rev(ch[x][1]),rev[x]=0;
}
il vd rotate(int x){
int y=fa[x],z=fa[y],o=x==ch[y][1];
fa[x]=z;if(!isrt(y))ch[z][y==ch[z][1]]=x;
ch[y][o]=ch[x][!o];fa[ch[x][!o]]=y;
fa[y]=x;ch[x][!o]=y;
upd(y);
}
il vd splay(int x){
if(x==0)return;
down(x);
for(int y=fa[x],z=fa[y];!isrt(x);rotate(x),y=fa[x],z=fa[y])
if(!isrt(y))rotate(((x==ch[y][0])!=(y==ch[z][0]))?x:y);
upd(x);
}
il vd access(int x){for(int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y,upd(x);}
il vd makert(int x){access(x),splay(x),Rev(x);}
makert(x);fa[x]=y;
}
il vd cut(int x,int y){
makert(x),access(y),splay(y);ch[y][0]=fa[x]=0;
}
il std::pair<int,int> dist(int x,int y){
makert(x),access(y),splay(y);
int z=y;while(ch[z][0])z=ch[z][0];
if(z!=x)return std::make_pair(-1,-1);
else return std::make_pair(siz[y]-1,mn[y]);
}
std::set<int>edgeList;
bool yes[maxn];
int main(){
int n=gi(),m=gi(),T=gi(),cnt=0;
for(int i=1;i<=m;++i){
int u=gi(),v=gi(),st=gi()+1,ed=gi()+1;
if(st==ed)continue;
E[++cnt]=(edge){u,v,st,ed,1,i};
E[++cnt]=(edge){u,v,ed,ed,-1,i};
}
std::stable_sort(E+1,E+cnt+1);
int p=1;
for(int i=0;i<=n;++i)w[i]=1e9;
for(int t=1;t<=T;++t){
while(p<=cnt&&E[p].t<=t){
if(E[p].o==-1){
std::set<int>::iterator it=edgeList.find(E[p].id);
if(it!=edgeList.end())edgeList.erase(it);
else if(!yes[E[p].id])cut(E[p].u,n+E[p].id),cut(E[p].v,n+E[p].id);
}else{
std::pair<int,int> d=dist(E[p].u,E[p].v);
if(E[p].u==E[p].v)edgeList.insert(E[p].id);
else if(d.first==-1){
int x=E[p].id+n;
w[x]=E[p].ed;
}
else if(w[d.second]<E[p].ed){
int x=n+E[p].id;
w[x]=E[p].ed;
cut(d.second,A[d.second]),cut(d.second,B[d.second]);
if(d.first%4==2)yes[d.second-n]=1;
else edgeList.insert(d.second-n);
A[x]=E[p].u,B[x]=E[p].v;
}
else if(d.first%4==0)edgeList.insert(E[p].id);
else yes[E[p].id]=1;
}
++p;
}
if(edgeList.empty())puts("Yes");
else puts("No");
}
return 0;
}


posted @ 2018-07-26 12:22  菜狗xzz  阅读(117)  评论(0编辑  收藏  举报