813F - Bipartite Checking - CodeForces Solution


data structures dsu graphs *2500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector <pair<int,int> > vd[500005],vs[500005];
int t[100005],dist[100005],sz[100005],ras[100005],n,q;
pair<int,int> v[100005];
map<pair<int,int>,int> ma;
void query11(int st,int dr,int nod,int qa,int qb,pair<int,int> val)
{
    if (qa<=st&&dr<=qb)
    {
        vd[nod].push_back(val);
        return ;
    }
    int mij=(st+dr)/2;
    if (qa<=mij) query11(st,mij,nod*2,qa,qb,val);
    if (qb>mij) query11(mij+1,dr,nod*2+1,qa,qb,val);
}
pair<int,int> rep(int a,int sum)
{
    if (t[a]==a) return make_pair(a,sum);
    return rep(t[a],sum+dist[a]);
}
int united(int a,int b,int nod)
{
    pair<int,int> x=rep(a,0),y=rep(b,0);
    if (x.first!=y.first){
    vs[nod].push_back({x.first,y.first});
    if (sz[x.first]>sz[y.first]) swap(x,y);
    t[x.first]=y.first;
    sz[y.first]+=sz[x.first];
    if ((x.second+y.second)%2==0) dist[x.first]=1;
    return 0;
    }
    else
    {
        if ((x.second+y.second)%2==0) return 1;
        return 0;
    }
}
void ununite(int a,int b)
{
    if (t[a]!=b&&t[b]!=a) return ;
    if (t[b]==a) swap(a,b);
    t[a]=a;
    sz[b]-=sz[a];
    dist[a]=0;
}
void divd(int st,int dr,int nod)
{
    int ok=0;
    for (int i=0;i<vd[nod].size();++i)
    {
        ok=(ok|united(vd[nod][i].first,vd[nod][i].second,nod));
    }
    if (ok==0)
    {
        if (st==dr) {ras[st]=1;}
        else
        {
        int mij=(st+dr)/2;
        divd(st,mij,nod*2);
        divd(mij+1,dr,nod*2+1);
        }
    }
    for (int i=vs[nod].size()-1;i>=0;--i)
    {
        ununite(vs[nod][i].first,vs[nod][i].second);
    }

}
int main()
{
    cin>>n>>q;
    for (int i=1;i<=n;++i)
    {
        t[i]=i;
        sz[i]=1;
        dist[i]=0;

    }
    for (int i=1;i<=q;++i)
    {
        cin>>v[i].first>>v[i].second;
        if (ma[v[i]]==0) {ma[v[i]]=i;}
        else {query11(1,q,1,ma[v[i]],i-1,v[i]); ma[v[i]]=0;}
    }
    for (auto it:ma)
    {
        if (ma[it.first])
        {
            query11(1,q,1,it.second,q,it.first);
            ma[it.first]=0;
        }
    }
    divd(1,q,1);
    for (int i=1;i<=q;++i)
    {
        if (ras[i]==1) cout<<"YES";
        else cout<<"NO";
        cout<<'\n';
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String