suoj33 诡异的交通(lct+stl)

版权声明:转载附上原文地址即可~欢迎各位神犇来虐~ https://blog.csdn.net/Icefox_zhx/article/details/79647670

orz sbw
题目传送门:portal

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <utility>
#include <map>
using namespace std;
#define ll long long
#define N 100010
#define inf 0x3f3f3f3f
#define pa pair<int,int>
inline int read(){
    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;
}
int n,m,fa[N*6],c[N*6][2],owo=0,mn[N*6],v[N*6],q[N*6];
struct oper{
    int op,x,y;
}qq[N<<2];map<pa,int>mp;
bool mark[N<<2],rev[N*6];
inline bool isroot(int x){return x!=c[fa[x]][0]&&x!=c[fa[x]][1];}
inline void update(int p){
    int l=c[p][0],r=c[p][1];mn[p]=p;
    if(v[mn[l]]<v[mn[p]]) mn[p]=mn[l];
    if(v[mn[r]]<v[mn[p]]) mn[p]=mn[r];
}
inline void pushdown(int p){
    if(!rev[p]) return;rev[p]=0;swap(c[p][0],c[p][1]);
    rev[c[p][0]]^=1;rev[c[p][1]]^=1;
}
inline void rotate(int x){
    int y=fa[x],z=fa[y],l=x==c[y][1],r=l^1;
    if(!isroot(y)) c[z][y==c[z][1]]=x;
    fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
    c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x){
    int top=0;q[++top]=x;
    for(int xx=x;!isroot(xx);xx=fa[xx]) q[++top]=fa[xx];
    while(top) pushdown(q[top--]);
    while(!isroot(x)){
        int y=fa[x],z=fa[y];
        if(!isroot(y)){
            if(x==c[y][1]^y==c[z][1]) rotate(x);
            else rotate(y);
        }rotate(x);
    }
}
inline void access(int x){
    int y=0;while(x){splay(x);c[x][1]=y;update(x);y=x;x=fa[x];}
}
inline void makeroot(int x){
    access(x);splay(x);rev[x]^=1;
}
inline void link(int x,int y){makeroot(x);fa[x]=y;}
inline void cut(int x,int y){
    makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;update(y);
}
inline int qmin(int x,int y){
    makeroot(x);access(y);splay(y);return mn[y];
}
inline int find(int x){
    access(x);splay(x);while(c[x][0]) x=c[x][0];return x;
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=1;i<=3*n;++i){
        int x=read(),y=read(),op=read();if(x>y) swap(x,y);
        if(!op) continue;qq[++owo].x=x;qq[owo].y=y;
        qq[owo].op=1;mp[make_pair(x,y)]=owo;
    }n<<=1;for(int i=0;i<=n;++i) v[i]=inf;
    while(m--){
        char op[10];scanf("%s",op+1);int x=read(),y=read();if(x>y) swap(x,y);
        if(op[1]=='O'){
            qq[++owo].op=1;qq[owo].x=x;qq[owo].y=y;
            mp[make_pair(x,y)]=owo;continue;
        }if(op[1]=='A'){qq[++owo].op=3;qq[owo].x=x;qq[owo].y=y;continue;}
        qq[++owo].op=2;qq[owo].x=x;qq[owo].y=y;v[owo+n]=mp[make_pair(x,y)];
        v[v[owo+n]+n]=owo;
    }m=owo;for(int i=1;i<=m;++i) if(qq[i].op==1&&!v[i+n]) v[i+n]=m+1;
    for(int i=1;i<=m;++i){
        int x=qq[i].x,y=qq[i].y;
        if(qq[i].op==1){
            if(find(x)==find(y)){
                int z=qmin(x,y);if(v[z]>=v[i+n]) continue;
                cut(qq[z-n].x,z);cut(z,qq[z-n].y);mark[z-n]=0;
                link(i+n,x);link(y,i+n);mark[i]=1;
            }else link(x,i+n),link(i+n,y),mark[i]=1;continue;
        }if(qq[i].op==2){
            int id=v[i+n];
            if(!mark[id]) continue;
            cut(qq[id].x,id+n);cut(id+n,qq[id].y);continue;
        }puts(find(x)==find(y)?"1":"0");
    }return 0;
}
展开阅读全文

没有更多推荐了,返回首页