noip2019 training test events (four) C.graph

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/tylon2006/article/details/100556029

Description

After a given one of n points without edges to FIG. M, deleting each point Q, a bipartite graph is not original.


Input

Input files contain multiple sets of data, the beginning of the file given data set number T.

For each test:

The first line of two integers n, m.

Next m lines of two integers u, v, shown in the drawing has edge (u, v), to ensure that u ≠ v.


Output

For each test, output a line character string length n s, si = 0 indicates the i-th point picture delete not bipartite graph, si = 1 is represented by the bipartite graph to delete.


Solution

Magic CDQ partition.

We must first know the definition of a bipartite graph: No odd ring of Figure

We divide and conquer n points to keep adding edge to remove a point way to determine the feasibility.
Setting an empty set S, and a graph G (initially empty), each partition l l time interval, the r r interval into the S, is added in FIG. r r edges between points S within the range of points and each time whether there are odd check ring bordered FIG.

Similarly, it is to be noted in FIG restore to the original state when the partition section r.

How to determine whether the odd ring:

We maintain a merger by the sub-tree size disjoint-set, and then save the parity of the number of layers for each point.
The default is the root node 0, and 1010 alternately.

X to y was added while there are two cases:

  1. x and y are in the same sub-tree:
    this case is determined whether x and y of the same color, even if it the same singularity as a ring (self-drawing), not directly connected to the same side.

  2. x and y are in the same subtree:
    a direct link side.

Note by rank can not merge path compression.


Code

#include<bits/stdc++.h>
using namespace std;
int f[100010],to[200010],head[100010],nxt[200010],siz[100010],col[100010],top,cnt,n,m;
char ans[100010];
struct data{
    int u,v,fau,fav,colu,colv,sizu,sizv;
}stk[100010],tmp;
void add(int x,int y){
    ++cnt;
    to[cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt; 
}
int find(int x){
    if(x!=f[x]) return find(f[x]);
    return x;
}
int findcol(int x){//得到当前节点颜色
    if(x==f[x]) return 0;
    return col[x]^findcol(f[x]); 
}
bool merge(int x,int y){
    int fax=find(x),fay=find(y);
    int colx=findcol(x),coly=findcol(y);
    if(fax==fay){
        if(colx==coly) return 0;
        return 1;
    }
    int fa=fax,son=fay;
    if(siz[fax]<siz[fay]) swap(fa,son);
    stk[++top]=(data){fa,son,f[fa],f[son],col[fa],col[son],siz[fa],siz[son]};
    if(colx==coly) col[son]^=1;
    siz[fa]+=siz[son];
    f[son]=fa;
    return 1;
}
bool work(int l,int r,int ll,int rr){//加边
    for(int i=l;i<=r;i++)
    for(int j=head[i];j;j=nxt[j]){
        if(ll<=to[j]&&to[j]<=rr) continue;
        if(!merge(i,to[j])) return 0;
    }
    return 1;
} 
void init(int x){//还原
    while(top>x){
        tmp=stk[top]; top--;
        f[tmp.u]=tmp.fau;
        f[tmp.v]=tmp.fav;
        col[tmp.u]=tmp.colu;
        col[tmp.v]=tmp.colv;
        siz[tmp.u]=tmp.sizu;
        siz[tmp.v]=tmp.sizv;
    }
} 
void solve(int l,int r,bool flag){
    //cout<<l<<" "<<r<<endl;
    if(l==r){
        ans[l]=flag+'0';
        return;
    }
    int last=top,mid=(l+r)>>1;
    bool now=flag&&work(mid+1,r,l,mid);
    solve(l,mid,now); init(last);
    now=flag&&work(l,mid,mid+1,r);
    solve(mid+1,r,now); init(last);
}
int main(){
    int x,y,t;
    scanf("%d",&t);
    while(t--){
        top=cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            head[i]=col[i]=0;
            siz[i]=1;
            f[i]=i;
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d",&x,&y);
            add(x,y),add(y,x);
        }
        ans[n+1]=0; //只是为了不多输出
        solve(1,n,1);
        printf("%s\n",ans+1);   
    }
}

Guess you like

Origin blog.csdn.net/tylon2006/article/details/100556029