Submission #450457

# Submission time Handle Problem Language Result Execution time Memory
450457 2021-08-02T23:25:16 Z mjhmjh1104 Joker (BOI20_joker) C++17
0 / 100
2000 ms 7580 KB
#include <tuple>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;

int uf[200006], sz[200006], ds[200006], y[200006];

int _find(int x) {
    if (uf[x] == -1) return x;
    return _find(uf[x]);
}

int _dist(int x) {
    if (uf[x] == -1) return 0;
    return ds[x] + _dist(uf[x]);
}

vector<tuple<int, int, int>> st;

bool _merge(int x, int y) {
    int a = _dist(x), b = _dist(y);
    x = _find(x), y = _find(y);
    if (x != y) {
        if (sz[x] < sz[y]) swap(x, y);
        st.push_back({ 0, y, uf[y] });
        uf[y] = x;
        if (sz[x] == sz[y]) {
            st.push_back({ 1, x, sz[x] });
            sz[x]++;
        }
        if ((a + b) % 2 == 0) {
            st.push_back({ 2, y, ds[y] });
            ds[y]++;
        }
        return true;
    }
    return false;
}

void _rollback() {
    auto [ a, b, c ] = st.back(); st.pop_back();
    if (a == 0) uf[b] = c;
    if (a == 1) sz[b] = c;
    if (a == 2) ds[b] = c;
}

int n, m, q;
vector<pair<int, int>> edges;

void dnc(int b, int e, int l, int r) {
    if (b > e) return;
    int x = -2;
    int s = (int)st.size();
    for (int i = b; i < m; i++) if (!_merge(edges[i].first, edges[i].second) && (_dist(edges[i].first) + _dist(edges[i].second) + 1) % 2) {
        x = r;
        break;
    }
    if (x == -2) for (int i = r; i > l; i--) if (!_merge(edges[i].first, edges[i].second) && (_dist(edges[i].first) + _dist(edges[i].second) + 1) % 2) {
        x = i;
        break;
    }
    if (x == -2) x = l;
    int m = (b + e) / 2;
    while ((int)st.size() > s) _rollback();
    y[m] = x;
    dnc(b, m - 1, l, x);
    _merge(edges[m].first, edges[m].second);
    dnc(m + 1, e, x, r);
}

int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        edges.push_back({ u, v });
    }
    for (int i = 0; i < n; i++) uf[i] = -1, sz[i] = 0, ds[i] = 0;
    dnc(0, m - 1, -1, m - 1);
    for (int i = 0; i < q; i++) {
        int l, r;
        scanf("%d%d", &l, &r);
        l--, r--;
        if (r < y[l]) puts("YES");
        else puts("NO");
    }
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d%d%d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
Joker.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         scanf("%d%d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Execution timed out 2075 ms 7580 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -