Submission #649802

# Submission time Handle Problem Language Result Execution time Memory
649802 2022-10-11T11:00:03 Z welleyth Joker (BOI20_joker) C++17
0 / 100
91 ms 27452 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

//#define int long long
#define pb push_back
#define mp make_pair

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

constexpr int N = (int)2e5 + 111;
constexpr int md = (int)2e5 + 111;

mt19937 rnd(time(nullptr));

struct DSU{
    int p[N];
    int deep[N];
    vector<int> vertexes[N];
    int sz[N];
    DSU(){}
    void init(){
        for(int i = 0; i < N; i++){
            p[i] = i;
            deep[i] = 0;
            sz[i] = 1;
            vertexes[i].clear();
            vertexes[i].pb(i);
        }
    }
    int get(int x){
        return p[x] == x ? x : p[x] = get(p[x]);
    }
    void union_sets(int a,int b){
        a = get(a), b = get(b);
        if(a == b)
            return;
        if(vertexes[a].size() > vertexes[b].size())
            swap(a,b);
        p[a] = b;
        deep[b] = max(deep[b],deep[a]+1);
        sz[b] += sz[a];
        for(auto& x : vertexes[a])
            vertexes[b].pb(x);
    }
    bool connected(int a,int b){
        return get(a) == get(b);
    }
};

vector<int> g[N];
bool used[2][N];
bool c[2][N];

bool dfs(int v,int r,int pr = -1){
    used[r][v] = true;
    c[r][v] = true;
    for(auto& to : g[v]){
        if(pr == to)
            continue;
        if(used[r^1][to]){
            if(c[r][v]&c[r][to])
                return true;
            continue;
        }
        c[r^1][to] = true;
        if(dfs(to,r^1,v))
            return true;
    }
    return false;
}

void solve(){
    int n,m,q;
    cin >> n >> m >> q;

    vector<pair<int,int>> edges;
    for(int i = 0; i < m; i++){
        int a,b;
        cin >> a >> b;
        edges.pb(mp(a,b));
    }

    bool answer[q];
    DSU d;

    vector<pair<int,int>> Queries[m+1];

    for(int i = 0; i < q; i++){
        int l,r;
        cin >> l >> r;
        Queries[l].pb(mp(r,i));
    }
    for(int i = 0; i <= m; i++){
        if(Queries[i].empty())
            continue;
        sort(Queries[i].rbegin(),Queries[i].rend());
        for(int j = 0; j <= n; j++){
            used[0][j] = false;
            used[1][j] = false;
            c[0][j] = c[1][j] = 0;
            g[j].clear();
        }
        d.init();
        for(int j = 0; j < i-1; j++){
            auto&[a,b] = edges[j];
            g[a].pb(b);
            g[b].pb(a);
            d.union_sets(a,b);
        }
        int pr = m;
        bool have = false;
        for(int i = 1; i <= n; i++){
            if(!g[i].empty() && !used[0][i] && !used[1][i]){
                have |= dfs(i,0);
            }
        }
        for(auto&[r,id] : Queries[i]){
            for(int j = r; j < pr && !have; j++){
                auto&[a,b] = edges[j];
                g[a].pb(b);
                g[b].pb(a);
                if(!d.connected(a,b) && ((used[0][a] && used[0][b]) || (used[1][a] && used[1][b]))){
                    if(d.vertexes[a] < d.vertexes[b]){
                        for(auto& v : d.vertexes[a]){
                            swap(used[0][v],used[1][v]);
                            swap(c[0][v],c[1][v]);
                        }
                    } else {
                        for(auto& v : d.vertexes[b]){
                            swap(used[0][v],used[1][v]);
                            swap(c[0][v],c[1][v]);
                        }
                    }
                    d.union_sets(a,b);
                } else {
                    if(!used[0][a] && !used[1][a]){
                        have |= dfs(a,0);
                    } else {
                        have |= c[0][a]&c[0][b];
                        have |= c[1][a]&c[1][b];
                    }
                }
            }
            pr = r;
            answer[id] = have;
        }
    }

    for(int i = 0; i < q; i++){
        cout << (answer[i] ? "YES" : "NO") << "\n";
    }

    return;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
//    init();
    int tests = 1;
//    cin >> tests;
    for(int test = 1; test <= tests; test++){
//        cerr << "test = " << test << "\n";
        solve();
    }
    return 0;
}
/**
6 8 2
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
4 7
4 8

**/
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Correct 15 ms 18348 KB Output is correct
4 Incorrect 26 ms 18356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Correct 15 ms 18348 KB Output is correct
4 Incorrect 26 ms 18356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Incorrect 91 ms 27452 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Correct 15 ms 18348 KB Output is correct
4 Incorrect 26 ms 18356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Correct 15 ms 18348 KB Output is correct
4 Incorrect 26 ms 18356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18260 KB Output is correct
2 Correct 15 ms 18296 KB Output is correct
3 Correct 15 ms 18348 KB Output is correct
4 Incorrect 26 ms 18356 KB Output isn't correct
5 Halted 0 ms 0 KB -