您的位置:首页 > 其它

4025: 二分图 分治+并查集

2016-04-28 18:51 246 查看
分治大法吼啊..

好像动态图可以用LCT搞搞..不过没研究过 以后再说

分治的方法可以看popoqqq..感觉挺神的

顺便了解了一下命名空间..

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

const int N=100005;
int n,m,T,top;
int stack[N<<2];
struct edge
{
int x,y,st,en;
edge () {};
edge (int a,int b,int c,int d) {x=a,y=b,st=c,en=d;}
};

inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}

namespace ufs
{
int f
,rank
,a
;

inline int find(int x)
{
for (;f[x]!=x;x=f[x]);
return x;
}

inline int dis(int x)
{
int res=0;
for (;f[x]!=x;res^=a[x],x=f[x]);
return res;
}

inline void Union(int x,int y,int z)
{
if (rank[x]>rank[y]) swap(x,y);
if (rank[x]==rank[y])
rank[y]++,stack[++top]=-y;
f[x]=y; a[x]=z; stack[++top]=x;
}

inline void restore(int tt)
{
while (top>tt)
{
if (stack[top]<0)
rank[-stack[top]]--;
else
f[stack[top]]=stack[top],a[stack[top]]=0;
top--;
}
}
}

void solve(int x,int y,vector<edge> e)
{
using namespace ufs;
vector<edge>::iterator it;
vector<edge> l,r;
int mid=x+y>>1,tt=top;
for (it=e.begin();it!=e.end();it++)
{
if (it->st==x&&it->en==y)
{
int p=find(it->x),q=find(it->y);
int d=dis(it->x)^dis(it->y)^1;
if (p!=q) Union(p,q,d);
else if (d&1)
{
for (int i=x;i<=y;i++) puts("No");
restore(tt);
return;
}
}
else if (it->en<=mid)
l.push_back(*it);
else if (it->st>mid)
r.push_back(*it);
else
l.push_back(edge(it->x,it->y,it->st,mid)),r.push_back(edge(it->x,it->y,mid+1,it->en));
}
if (x==y) puts("Yes");
else solve(x,mid,l),solve(mid+1,y,r);
restore(tt);
}

int main()
{
vector<edge> v;
n=read(); m=read(); T=read();
for (int i=1;i<=n;i++)
ufs::f[i]=i;
for (int i=1;i<=m;i++)
{
edge e;
e.x=read(); e.y=read(); e.st=read()+1; e.en=read();
if (e.st>e.en) continue;
v.push_back(e);
}
solve(1,T,v);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: