BZOJ 4025 bipartite FIG.

I heard that a good title is fed out oier

The meaning of problems

m edges n points, the total time is T.

While there exists a time every day. For each time I asked, whether the figure is a bipartite graph.

Problem-solving ideas

Learned graph theory, we know that a graph is bipartite graph if and only if he did odd ring.

Obviously if two points are none, then we can connect directly to the side in which, regardless of other conditions. So we need to know a disjoint-set.

I began to see a side disappears, then the autistic.

After dalao advice, find a kind called revocable disjoint-set thing.

Taking into account the present time each side, which can be expressed as log n nodes of the tree segments.

Traversing the tree line again, then added with the current node. Recursive about his son, and then revoked side.

Judge to consider violence odd ring jump, will hang. A set of heuristic merge.

Heuristic merger must be connected between the root and the root side, so we're going to deal with even a slight edge, the edge weight altered, can be connected directly equivalent to the edge.

Code

#include<bits/stdc++.h>
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
const int sz=2e5+527;
int n,m,T;
int dep[sz],f[sz],d[sz];
queue<int>q[sz<<1];
int getfa(int x) { return x==f[x]?x:getfa(f[x]); }
int getdis(int x) { return x==f[x]?0:d[x]^getdis(f[x]); }
struct Edge{
    int u,v,w;
}e[sz];
void insert(int o,int l,int r,int x,int y,int num){
    if(x<=l&&r<=y) return (void)(q[o].push(num));
    int mid=(l+r)>>1;
    if(x<=mid) insert(lson,x,y,num);
    if(mid<y) insert(rson,x,y,num);
}
void resum(stack<Edge>S){
    while(!S.empty()){
        d[f[S.top().u]=S.top().u]=0;
        dep[S.top().v]-=S.top().w;
        S.pop();
    }
}
void solve(int o,int l,int r){
    stack<Edge>S;
    while(!q[o].empty()){
        int num=q[o].front();
        q[o].pop();
        int x=e[num].u,y=e[num].v;
        int fx=getfa(x),fy=getfa(y);
        int w=getdis(x)^getdis(y)^1;
        if(fx==fy&&w){
            for(int i=l;i<=r;i++) puts("No");
            return (void)(resum(S));
        }
        if(fx!=fy){
            if(dep[fx]>dep[fy]) swap(fx,fy),swap(x,y);
            Edge cur=(Edge){fx,fy,0};
            f[fx]=fy,d[fx]=w;
            if(dep[fx]==dep[fy]) ++dep[fy],cur.w=1;
            S.push(cur);
        }
    }
    if(l==r){ resum(S);return (void)(puts("Yes")); }
    int mid=(l+r)>>1;
    solve(lson);
    solve(rson);
    resum(S);
}
int main(){
    scanf("%d%d%d",&n,&m,&T);
    int u,v,L,R;
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d%d",&u,&v,&L,&R);
        L++;
        e[i]=(Edge){u,v,1};
        insert(1,1,T,L,R,i);
    }
    solve(1,1,T);
}

Guess you like

Origin www.cnblogs.com/river-flows-in-you/p/11286120.html