답안 #689327

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
689327 2023-01-28T07:30:12 Z zeroesandones Joker (BOI20_joker) C++17
0 / 100
101 ms 23308 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define FOR(i, j, k) for (ll i = j; i < (ll) k; ++i)
#define FORD(i, j, k) for (ll i = j; i >= (ll) k; --i)
#define nl "\n"
#define sp " "

#define all(x) (x).begin(), (x).end()
#define sc second
#define fr first
#define pb emplace_back

struct state {
    ll u, v;
    ll szu, szv;
    ll lenu, lenv;
    bool cyc;
};

struct UFDS {
    vi link, sz, len;
    bool cyc;
    stack<state> s;
    int n;

    UFDS(int N) {
        init(N);
    }

    void init(int N) {
        n = N;
        link = vi(n);
        iota(all(link), 0);
        sz = vi(n, 1);
        len = vi(n, 0);
        cyc = false;
    }

    pi find(ll x) {
        if(link[x] == x)
            return {x, 0};
        pi val = find(link[x]);
        return {val.fr, (val.sc + 1) % 2};
    }

    void unite(ll a, ll b) {
        pi A = find(a);
        pi B = find(b);
        if(A.fr == B.fr) {
            if(A.sc == B.sc) {
                cyc = true;
            }
            return;
        }
        if(sz[A.fr] < sz[B.fr]) swap(A, B);

        state curr;
        curr.u = A.fr;
        curr.v = B.fr;
        curr.cyc = cyc;
        curr.szu = sz[A.fr];
        curr.szv = sz[B.fr];
        curr.lenu = len[A.fr];
        curr.lenv = len[B.fr];
        s.push(curr);
        link[B.fr] = A.fr;
        len[B.fr] = (1 + A.sc + B.sc) % 2;
        sz[A.fr] += sz[B.fr];
    }

    void check() {
        state c;
        c.u = -1;
        s.push(c);
    }

    void rollback() {
        while(!s.empty()) {
            state curr = s.top();
            s.pop();
            if(curr.u == -1) break;
            link[curr.u] = curr.u;
            link[curr.v] = curr.v;
            cyc = curr.cyc;
            sz[curr.u] = curr.szu;
            sz[curr.v] = curr.szv;
            len[curr.u] = curr.lenu;
            len[curr.v] = curr.lenv;
        }
    }

    bool same(ll a, ll b){
        return (find(a) == find(b));
    }
};

void solve()
{
    // TODO: Add rollback to UFDS
    ll n, m, q;
    cin >> n >> m >> q;

    vector<pi> e(m + 1);
    FOR(i, 1, m + 1) {
        cin >> e[i].fr >> e[i].sc;
    }

    UFDS uf(n + 1);

    vector<pi> query[201];
    FOR(i, 0, q) {
        ll l, r;
        cin >> l >> r;
        query[l].pb(r, i);
    }
    vector<bool> ans(q, false);

    FOR(i, 0, 201) {
        sort(all(query[i]), [&](pi x, pi y) {
                return x.fr > y.fr;
                });
    }

    FOR(T, 1, 201) {
        if(T > m) break;
        if(T >= 2)
            uf.unite(e[T - 1].fr, e[T - 1].sc);
        uf.check();
        ll r = m;
        for(auto i : query[T]) {
            while(r > i.fr) {
                uf.unite(e[r].fr, e[r].sc);
                --r;
            }
            ans[i.sc] = uf.cyc;
        }
        uf.rollback();
    }

    for(auto i : ans) {
        cout << (i ? "YES" : "NO") << nl;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 87 ms 15224 KB Output is correct
4 Incorrect 101 ms 23308 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -