Submission #1225174

#TimeUsernameProblemLanguageResultExecution timeMemory
1225174yassiaJoker (BOI20_joker)C++20
49 / 100
820 ms18432 KiB
#ifndef local
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#endif
#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;
using ll = int;
using pll = pair<ll,ll>;
using str = string;
using ld = long double;
using hash_map =gp_hash_table<int, int>;
using hash_set= gp_hash_table <int, null_type>;
auto sd = std::chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(sd);
typedef tree<ll, null_type, less<>, rb_tree_tag,
        tree_order_statistics_node_update> ord_set;
const ll maxn = 3e5;
ll inv(ll c){
    if (c==1)return 2;
    return 1;
}
vector<vector<pll>> g;
ll cnt[maxn];
ll p[maxn];
ll sz[maxn];

pll get(ll a){
    if (a == p[a]){
        return {p[a], 0};
    }
    pll w = get(p[a]);
    p[a] = w.first;
    cnt[a] = (cnt[a] + w.second)%2;
    return {p[a],cnt[a]};
}
void union_(ll a, ll b, bool &is) {
    auto a1 = get(a);
    auto b1 = get(b);
    ll aa = a1.first;
    ll bb = b1.first;
    if (a1 == b1) {
        is = false;
        return;
    }
    else if (a1.first==b1.first && a1.second != b1.second){
        return;
    }
    if (sz[aa]<sz[bb]){
        swap(a1, b1);
        swap(a, b);
        swap(aa, bb);
    }
    sz[aa]+=sz[bb];
    p[bb] =aa;
    cnt[bb] = (a1.second+b1.second+1)%2;
}

void dfs(ll v, vector<ll>&col, ll c, bool &is, ll l, ll r){
    col[v] = c;
    for (auto u : g[v]){
        if (l <= u.second && u.second <= r){
            continue;
        }
        if (!col[u.first]){
            dfs(u.first, col, inv(c),is, l,r);
        }
        else if (c == col[u.first]){
            is = false;
            return;
        }
    }
}
void solve1() {
    ll n;
    cin >> n;
    ll m;
    cin >> m; ll q; cin >> q;
    vector<pll> edges;
    g.resize(n);
    for (int i =0; i < m; i++){
        int a, b;
        cin>>a>>b;
        a--; b--;
        edges.emplace_back(a, b);
        g[a].emplace_back(b, i);
        g[b].emplace_back(a, i);
    }
    vector<int>col(n);
    if (max(n, m)*q <= 4000000ll){
        for (int i = 0; i <q; i++){
            ll l, r;
            cin>>l>>r; l--; r--;
            col.assign(n, 0);
            bool is =true;
            for (int j = 0; j < n; j++){
                if (!col[j]){
                    dfs(j, col, 1, is, l, r);
                }
            }
            if (!is){
                cout<<"YES\n";
            }
            else {
                cout<<"NO\n";
            }
        }
    }
    else {
        vector<ll> ans(m);
        ll lf = 0;
        for (int i = 0; i < min(201, m); i++){
            ans[i] = i-1;
            for (int j = 0; j <= n; j++){
                sz[j] = 1;
                p[j] = j;
                cnt[j] = 0;
            }
            bool is = true;
            for (int k = 0; k < i; k++){
                union_(edges[k].first, edges[k].second, is);
            }
            if(!is) {
                ans[i] = m-1;
            }
            else {
                for (int j = m-1; j > i; j--){
                    union_(edges[j].first, edges[j].second, is);
                    if (!is) {
                        ans[i] = j-1;
                        break;
                    }
                }
            }
        }
        for (int i =0; i < q; i++){
            int l, r;
            cin>>l>>r;
            l--;
            r--;
            if (r<=ans[l]){
                cout<<"YES\n";
            }
            else cout<<"NO\n";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifdef local
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    solve1();
#ifdef local
    printf_s("\n%.5f s", (double) clock() / CLOCKS_PER_SEC);
#endif
}
#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...