제출 #1324249

#제출 시각아이디문제언어결과실행 시간메모리
1324249888313666Joker (BOI20_joker)C++20
0 / 100
277 ms13400 KiB
#include <bits/stdc++.h>    
using namespace std;
typedef long long ll;
#define _ <<' '<<
#define print(x) cout<<#x<<": "<<(x)<<'\n'

int n,m,q,k,b;
bool val;
vector<vector<array<int, 2>>> adj;
vector<int> bip;
vector<vector<array<int, 3>>> bk;
stack<array<int, 8>> s;
vector<array<int, 2>> eg;

bool operator<(const array<int, 3> &a, const array<int, 3> &b){
    if (a[1]>b[1]) return true;
    if (a[1]<b[1]) return false;
    if (a[0]<b[0]) return true;
    if (a[0]>b[0]) return false;
    if (a[2]<b[2]) return true;
    return false;
}

int block(const int i){
    return i/k;
}

struct dsu{
    int n;
    vector<int> par, sz, lz;

    dsu (const int n) {
        this->n=n;
        par.resize(n);
        iota(par.begin(), par.end(), 0);        
        sz.assign(n, 1);
        lz.assign(n, 0);
    }

    array<int, 2> find(const int i) {
        if (par[i]==i) return {i, lz[i]};
        auto f=find(par[i]);
        f[1]^=lz[i];
        return f;
    }

    bool unite(int i, int j) {
        int g, h;
        auto 一=find(i), 二=find(j);
        i=一[0], g=一[1], j=二[0], h=二[1];
        s.push({i, j, par[i], par[j], sz[i], sz[j], lz[i], lz[j]});
        if (i==j) {
            if (g==h) {
                return false;
            }
            return true;
        } else {
            if (sz[i]<sz[j]) swap(i, j);
            par[j]=i;
            sz[i]+=sz[j];
            lz[j]^=!lz[i];
            return true;
        }
        assert(false);
    }

    void clear(){
        while (!s.empty()) {
            const auto [q, w, e, r, t, y, u, i]=s.top();
            s.pop();
            par[q]=e;
            par[w]=r;
            sz[q]=t;
            sz[w]=y;
            lz[q]=u;
            lz[w]=i;
        }
    }
};

void dfs(const int u) {
    for (const auto [v, i]:adj[u]) if (true) {
        if (bip[v]==bip[u]) {
            //print(u);
            //print(v);
            //print(i);
            val=false;
        }
        else if (bip[v]==-1) {
            bip[v]=!bip[u];
            dfs(v);
        }
    } 
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    cin>>n>>m>>q;
    adj.resize(n);
    k=sqrt(m);
    b=block(m-1)+1;
    eg.resize(m);
    for (int i=0; i<m; i++) {
        int a,b;
        cin>>a>>b;
        adj[--a].push_back({--b, i});
        adj[b].push_back({a, i});
        eg[i]={a, b};
    }
    bk.resize(b);
    for (int i=0; i<q; i++) {
        int l,r;
        cin>>l>>r;
        bk[block(--l)].push_back({l, --r, i});
    }
    for (int i=0; i<b; i++) sort(bk.rbegin(), bk.rend());
    for (int i=0; i<b; i++) {
        auto ptr=m-1;
        dsu f(n);
        if (i) for (int j=0; j<=i*k-1; j++) {
            if (!f.unite(eg[j][0], eg[j][1])){
                s.pop();
                for (int w=i; w<b; ++w) for (const auto g:bk[w]) cout<<"YES\n";
                return 0;
            }
            s.pop();
        }
        bool vd=1;
        //print(b);
        //print(ptr);
        for (const auto [l, r, x]:bk[i]) {
            //print(ptr);
            //print(r);
            while (ptr>=i*k+1 && r<ptr) {
                //print(ptr);
                if (!f.unite(eg[ptr][0], eg[ptr--][1])) {
                    vd=0;
                    //print(vd);
                }
                s.pop();
            }
            if (!vd){cout<<"YES\n"; continue;}
            bool fg=1;
            for (int j=i*k; j<l; ++j) {
                //print(j);
                if (!f.unite(eg[j][0], eg[j][1])){
                    cout<<"YES\n";
                    fg=0;
                    break;
                }
            }
            if (fg) cout<<"NO\n";
            f.clear();
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...