Submission #969885

# Submission time Handle Problem Language Result Execution time Memory
969885 2024-04-25T18:14:34 Z RandomUser Pictionary (COCI18_pictionary) C++17
126 / 140
150 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

struct DSU {
    int n;
    vector<int> par, size;

    void config(int _n) {
        n = _n + 1;
        par.resize(n+1);
        size.resize(n+1, 1);
        for(int i=1; i<=n; i++) par[i] = i;
    }

    int get(int u) {
        if(u == par[u]) return u;
        return par[u] = get(par[u]);
    }

    bool uni(int u, int v) {
        u = get(u), v = get(v);

        if(u == v) return false;
        if(size[u] < size[v]) swap(u, v);

        size[u] += size[v];
        par[v] = u;

        return true;
    }

    bool sameSet(int u, int v) { return get(u) == get(v); }

    void reset() {
        for(int i=1; i<=n; i++) par[i] = i, size[i] = 1;
    }
};

int32_t main() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<pii> qus(q);
    for(pii &x : qus) cin >> x.first >> x.second;

    vector<int> L(q, 1), R(q, m), ANS(q, m);
    bool changed = 1;
    stack<int> to_check[m+1];
    DSU dsu; dsu.config(n);

    while(changed) {
        changed = 0;

        for(int i=0; i<q; i++) {
            if(L[i] <= R[i]) {
                to_check[(L[i] + R[i]) / 2].push(i);
                changed = 1;
            }
        }

        int curr = m;
        dsu.reset();
        for(int i=1; i<=m; i++) {
            for(int j=curr; j<=n; j+=curr)
                if(j+curr <= n) dsu.uni(j, j+curr);

            while(!to_check[i].empty()) {
                int id = to_check[i].top();
                to_check[i].pop();
                if(dsu.sameSet(qus[id].first, qus[id].second)) ANS[id] = i, R[id] = i - 1;
                else L[id] = i + 1;
            }

            curr--;
        }
        
    }

    for(int &x : ANS) cout << x << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 2644 KB Output is correct
2 Correct 33 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 3512 KB Output is correct
2 Correct 44 ms 2896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 16724 KB Output is correct
2 Correct 46 ms 7660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 14636 KB Output is correct
2 Correct 46 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 33268 KB Output is correct
2 Correct 74 ms 15440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 3156 KB Output is correct
2 Correct 130 ms 24228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 41048 KB Output is correct
2 Correct 150 ms 18716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 69 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -