답안 #450461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
450461 2021-08-02T23:30:18 Z mjhmjh1104 Joker (BOI20_joker) C++17
0 / 100
174 ms 7628 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 = -1;
    int s = (int)st.size();
    int m = (b + e) / 2;
    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 == -1) 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 == -1) x = l;
    while ((int)st.size() > s) _rollback();
    y[m] = x;
    dnc(b, m - 1, l, x);
    if (!_merge(edges[m].first, edges[m].second) && (_dist(edges[m].first) + _dist(edges[m].second) + 1) % 2) x = r;
    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);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 252 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 252 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 174 ms 7628 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 252 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 252 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 252 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -