codeforces 813F Bipartite Checking

http://www.elijahqi.win/2018/03/09/codeforces-813f-bipartite-checking/
F. Bipartite Checking
time limit per test

6 seconds
memory limit per test

256 megabytes
input

standard input
output

standard output

You are given an undirected graph consisting of n vertices. Initially there are no edges in the graph. Also you are given q queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color).
Input

The first line contains two integers n and q (2 ≤ n, q ≤ 100000).

Then q lines follow. ith line contains two numbers xi and yi (1 ≤ xi < yi ≤ n). These numbers describe ith query: if there is an edge between vertices xi and yi, then remove it, otherwise add it.
Output

Print q lines. ith line must contain YES if the graph is bipartite after ith query, and NO otherwise.
Example

input
Copy

3 5
2 3
1 3
1 2
1 2
1 2

output

YES
YES
NO
YES
NO

同bzoj4025 但我原来的方法不会搞了 所以选择学习了下icefox巨佬的方法

还是维护一颗最大生成树 树的权值是 这条边消失的时间 记录mark表示判断该边是否属于奇环或者偶环 或者仅仅是树边 每次如果新边消失时间更长则替换树上原有的边 每次有一个奇环就计数器+1 如果该操作是删除就正常处理 注意需要删除树边

#include<map>
#include<queue>
#include<cstdio>
#include<algorithm>
#define pa pair<int,int>
#define N 220000
#define inf 0x3f3f3f3f
using namespace std;
map<pa,int> mm;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
struct node{
    int id,op;
}qr[N];
struct node1{
    int x,y;
}eg[N];
int c[N][2],fa[N],b[N],v[N],size[N],q[N],top,sum,mark[N],n,m,Q;
bool rev[N];
inline bool isroot(int x){
    return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void pushdown(int x){
    if (!rev[x]) return;rev[x]^=1;
    int l=c[x][0],r=c[x][1];rev[l]^=1;
    swap(c[x][0],c[x][1]);rev[r]^=1;
}
inline void update(int x){
    int l=c[x][0],r=c[x][1];b[x]=x;
    size[x]=size[l]+size[r]+1;  
    if (v[b[l]]<v[b[x]]) b[x]=b[l];
    if (v[b[r]]<v[b[x]]) b[x]=b[r];
}
inline void rotate(int x){
    int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    if(!isroot(y)) c[z][c[z][1]==y]=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){
    q[top=1]=x;for (int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i];
    while(top) pushdown(q[top--]);
    while(!isroot(x)){
        int y=fa[x],z=fa[y];
        if (!isroot(y)){
            if (c[y][0]==x^c[z][0]==y) rotate(x);else rotate(y);
        }rotate(x);
    }
}
inline void access(int x){
    for (int t=0;x;t=x,x=fa[x]) splay(x),c[x][1]=t,update(x);
}
inline int find(int x){
    access(x);splay(x);while(c[x][0]) x=c[x][0];return x;
}
inline void makeroot(int x){
    access(x);splay(x);rev[x]^=1;
}
inline 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[c[y][0]]=0;update(y);
}
int main(){
    freopen("cf813f.in","r",stdin);
    n=read();Q=read();int m=0;v[0]=inf;
    for(int i=1;i<=Q;++i){
        int x=read(),y=read();if(x>y) swap(x,y);int id=mm[make_pair(x,y)];
        if(id){qr[i].op=2;qr[i].id=id;mm[make_pair(x,y)]=0;v[n+id]=i;b[n+id]=n+id;continue;}
        qr[i].op=1;qr[i].id=++m;eg[m].x=x;eg[m].y=y;mm[make_pair(x,y)]=m;
    }
    for (int i=1;i<=n;++i) v[i]=inf,b[i]=i;
    for (int i=1;i<=m;++i) if (!v[i+n]) {v[i+n]=Q+1;b[i+n]=i+n;}
    for (int i=1;i<=Q;++i){
        int id=qr[i].id;int x=eg[id].x,y=eg[id].y;
        if (qr[i].op==1){
            if (find(y)!=find(x)) link(x,id+n),link(y,id+n);else{
                makeroot(x);access(y);splay(y);int tt=b[y],sz=size[y];
                if ((sz-1>>1)&1){
                    if (v[id+n]>v[tt]) cut(eg[tt-n].x,tt),cut(eg[tt-n].y,tt),link(x,id+n),link(y,id+n),mark[tt-n]=2;else mark[id]=2;
                }else{
                    ++sum;
                    if (v[id+n]>v[tt]) cut(eg[tt-n].x,tt),cut(eg[tt-n].y,tt),link(x,id+n),link(y,id+n),mark[tt-n]=1;else mark[id]=1;
                }
            }
        }else{if (!mark[id]) cut(x,id+n),cut(y,id+n);if (mark[id]==1) --sum;}
        if (sum) puts("NO");else puts("YES");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值