Submission #768832

# Submission time Handle Problem Language Result Execution time Memory
768832 2023-06-28T17:43:48 Z oyber Joker (BOI20_joker) C++14
71 / 100
2000 ms 14068 KB
#pragma GCC optimize("O3")
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
using namespace std;

vector<int> u;
vector<int> s;
vector<int> p;

int find(int n) {
    if (u[n] == n) return n;
    return find(u[n]);
}

int find_compress(int n) {
    int r = n;
    int z = 0; 
    while (r != u[r]) {
        r = u[r];
        z ^= p[r];
    }
    while (n != r) {
        if (z != p[r]) p[n] = !p[n];
        z ^= p[u[n]];
        int next_n = u[n];
        u[n] = r;
        n = next_n;
    }
    return r;
}

int parity(int n) {
    if (u[n] == n) return 0;
    return parity(u[n])^p[n];
}

struct UniteInfo {
    int a;
    int b;
};

UniteInfo unite(int a, int b) {
    int ap = find(a);
    int bp = find(b);
    if (s[bp] > s[ap]) {
        swap(a, b);
        swap(ap, bp);
    }
    u[bp] = ap;
    s[ap] += s[bp];
    if (parity(a) == parity(b)) {
        p[bp] = 1;
    } else {
        p[bp] = 0;
    }
    return UniteInfo {
        ap,
        bp,
    };
}

void undo(UniteInfo info) {
    u[info.b] = info.b;
    s[info.a] -= s[info.b];
    p[info.b] = 0;
}

struct Query {
    int l;
    int r;
    int i;
};

bool comp_query(Query q1, Query q2) {
    return q1.r > q2.r;
}

bool comp_query2(Query q1, Query q2) {
    return q1.l < q2.l;
}

int main() {
    int n, m, qn;
    scanf("%d %d %d", &n, &m, &qn);

    u.resize(n);
    s.resize(n, 1);
    p.resize(n);


    vector<pair<int, int>> e(m);
    for (int i = 0; i < m; i++) {
        int a, b; 
        scanf("%d %d", &a, &b);
        a--;b--;
        e[i] = make_pair(a, b);
        //printf("%d %d %d\n", a, b, i);
    }

    vector<Query> q(qn);
    for (int i = 0; i < qn; i++) {
        int l, r;
        scanf("%d %d", &l, &r);
        l--;r--;
        q[i] = Query{l,r,i};
    }
    sort(q.begin(), q.end(), comp_query2);

    int group_size = 1024;
    int group_num = q[q.size()-1].l / group_size + 1;
    //printf("%d %d\n", int(q[q.size()-1].l), group_num);
    vector<vector<Query>> groups(group_num);
    vector<int> min_l(group_num, 1000000);
    //printf("n: %d\n", group_num);
    int j = 0;
    for (int i = 0; i < group_num; i++) {
        while (j < qn) {
            if (q[j].l >= (i+1)*group_size) break;
            groups[i].push_back(q[j]);
            min_l[i] = min(min_l[i], q[j].l);
            //printf("g: %d q: %d\n", i, q[j].i);
            j++;
        }
        sort(groups[i].begin(), groups[i].end(), comp_query);
        //printf("%d\n", min_l[i]);
    }

    vector<UniteInfo> infos;
    vector<bool> result(qn);
    for (int j = 0; j < group_num; j++) {
        if (groups[j].size() == 0) continue;;

        //reset state
        for (int i = 0; i < n; i++) {
            u[i] = i;
            s[i] = 1;
            p[i] = 0;
        }

        bool l_cycle = false;
        int current_l = 0;
        for (; current_l < min_l[j]; current_l++) {
            int a = e[current_l].first;
            int b = e[current_l].second;
            //printf("%d %d %d\n", current_l, a, b);
            //printf("(%d %d) (%d %d)\n", find_compress(a), find_compress(b), find_compress2(a), find_compress2(b));
            if (find_compress(a) == find_compress(b)) {
                if (parity(a) == parity(b)) {
                    l_cycle = true;
                    break;
                }
            } else {
                unite(a, b);
            }
        }
        //printf("l: %d\n", current_l);


        int current_r = m-1;
        bool r_cycle = false;
        for (int i = 0; i < int(groups[j].size()); i++) {
            if (r_cycle || l_cycle) {
                result[groups[j][i].i] = true;
                continue;
            }
            int l = groups[j][i].l;
            int r = groups[j][i].r;
            //printf("%d: %d %d\n", i, l, r);

            while (current_r > r) {
                int a = e[current_r].first;
                int b = e[current_r].second;
                //printf("%d %d %d\n", current_r, a, b);
                /*int y1 = find_compress(a); 
                int z1 = find_compress(b); 
                int y2 = find_compress2(a); 
                int z2 = find_compress2(b); 
                if (y1 != y2 || z1 != z2) {
                    printf("(%d %d) (%d %d)\n", y1, z1, y2, z2);
                }*/
                if (find_compress(a) == find_compress(b)) {
                    if (parity(a) == parity(b)) {
                        r_cycle = true;
                        break;
                    }
                } else {
                    unite(a, b);
                }
                current_r--;
            }

            if (r_cycle) {
                result[groups[j][i].i] = true;
                continue;
            }

            bool cycle = false;
            for (int c_l = current_l; c_l < l; c_l++) {
                int a = e[c_l].first;
                int b = e[c_l].second;
                //printf("%d %d %d\n", current_l, a, b);
                if (find(a) == find(b)) {
                    if (parity(a) == parity(b)) {
                        cycle = true;
                        break;
                    }
                } else {
                    infos.push_back(unite(a, b));
                }
            }
            if (cycle) {
                result[groups[j][i].i] = true;
            }
            while (infos.size() > 0) {
                undo(infos.back());
                infos.pop_back();
            }
        }
    }

    for (int i = 0; i < qn; i++) {
        if (result[i]) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     scanf("%d %d %d", &n, &m, &qn);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
Joker.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 284 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 284 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 14 ms 412 KB Output is correct
30 Correct 8 ms 432 KB Output is correct
31 Correct 6 ms 340 KB Output is correct
32 Correct 5 ms 412 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 6 ms 356 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 7 ms 300 KB Output is correct
37 Correct 13 ms 360 KB Output is correct
38 Correct 17 ms 540 KB Output is correct
39 Correct 11 ms 352 KB Output is correct
40 Correct 6 ms 424 KB Output is correct
41 Correct 8 ms 296 KB Output is correct
42 Correct 6 ms 296 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 6 ms 412 KB Output is correct
48 Correct 7 ms 340 KB Output is correct
49 Correct 7 ms 340 KB Output is correct
50 Correct 7 ms 340 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 6 ms 340 KB Output is correct
53 Correct 7 ms 424 KB Output is correct
54 Correct 7 ms 428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 74 ms 8512 KB Output is correct
4 Correct 79 ms 9688 KB Output is correct
5 Correct 76 ms 11932 KB Output is correct
6 Correct 78 ms 10940 KB Output is correct
7 Correct 77 ms 11556 KB Output is correct
8 Correct 76 ms 11396 KB Output is correct
9 Correct 73 ms 11836 KB Output is correct
10 Correct 76 ms 13012 KB Output is correct
11 Correct 78 ms 11884 KB Output is correct
12 Correct 77 ms 12824 KB Output is correct
13 Correct 77 ms 10896 KB Output is correct
14 Correct 77 ms 11328 KB Output is correct
15 Correct 79 ms 12300 KB Output is correct
16 Correct 78 ms 13020 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 284 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 74 ms 8512 KB Output is correct
30 Correct 79 ms 9688 KB Output is correct
31 Correct 76 ms 11932 KB Output is correct
32 Correct 78 ms 10940 KB Output is correct
33 Correct 77 ms 11556 KB Output is correct
34 Correct 76 ms 11396 KB Output is correct
35 Correct 73 ms 11836 KB Output is correct
36 Correct 76 ms 13012 KB Output is correct
37 Correct 78 ms 11884 KB Output is correct
38 Correct 77 ms 12824 KB Output is correct
39 Correct 77 ms 10896 KB Output is correct
40 Correct 77 ms 11328 KB Output is correct
41 Correct 79 ms 12300 KB Output is correct
42 Correct 78 ms 13020 KB Output is correct
43 Correct 324 ms 11788 KB Output is correct
44 Correct 356 ms 12084 KB Output is correct
45 Correct 351 ms 12812 KB Output is correct
46 Correct 274 ms 11812 KB Output is correct
47 Correct 283 ms 12876 KB Output is correct
48 Correct 106 ms 12352 KB Output is correct
49 Correct 126 ms 13784 KB Output is correct
50 Correct 276 ms 12620 KB Output is correct
51 Correct 312 ms 13360 KB Output is correct
52 Correct 309 ms 14068 KB Output is correct
53 Correct 170 ms 11688 KB Output is correct
54 Correct 324 ms 12632 KB Output is correct
55 Correct 348 ms 13488 KB Output is correct
56 Correct 348 ms 14036 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 284 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 14 ms 412 KB Output is correct
30 Correct 8 ms 432 KB Output is correct
31 Correct 6 ms 340 KB Output is correct
32 Correct 5 ms 412 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 6 ms 356 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 7 ms 300 KB Output is correct
37 Correct 13 ms 360 KB Output is correct
38 Correct 17 ms 540 KB Output is correct
39 Correct 11 ms 352 KB Output is correct
40 Correct 6 ms 424 KB Output is correct
41 Correct 8 ms 296 KB Output is correct
42 Correct 6 ms 296 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 6 ms 412 KB Output is correct
48 Correct 7 ms 340 KB Output is correct
49 Correct 7 ms 340 KB Output is correct
50 Correct 7 ms 340 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 6 ms 340 KB Output is correct
53 Correct 7 ms 424 KB Output is correct
54 Correct 7 ms 428 KB Output is correct
55 Correct 496 ms 5424 KB Output is correct
56 Correct 1196 ms 6824 KB Output is correct
57 Correct 620 ms 6556 KB Output is correct
58 Correct 468 ms 5432 KB Output is correct
59 Correct 572 ms 5424 KB Output is correct
60 Correct 615 ms 6304 KB Output is correct
61 Correct 546 ms 5440 KB Output is correct
62 Correct 565 ms 6548 KB Output is correct
63 Correct 376 ms 4684 KB Output is correct
64 Correct 595 ms 5692 KB Output is correct
65 Correct 600 ms 6564 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 284 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 14 ms 412 KB Output is correct
30 Correct 8 ms 432 KB Output is correct
31 Correct 6 ms 340 KB Output is correct
32 Correct 5 ms 412 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 6 ms 356 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 7 ms 300 KB Output is correct
37 Correct 13 ms 360 KB Output is correct
38 Correct 17 ms 540 KB Output is correct
39 Correct 11 ms 352 KB Output is correct
40 Correct 6 ms 424 KB Output is correct
41 Correct 8 ms 296 KB Output is correct
42 Correct 6 ms 296 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 6 ms 412 KB Output is correct
48 Correct 7 ms 340 KB Output is correct
49 Correct 7 ms 340 KB Output is correct
50 Correct 7 ms 340 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 6 ms 340 KB Output is correct
53 Correct 7 ms 424 KB Output is correct
54 Correct 7 ms 428 KB Output is correct
55 Correct 74 ms 8512 KB Output is correct
56 Correct 79 ms 9688 KB Output is correct
57 Correct 76 ms 11932 KB Output is correct
58 Correct 78 ms 10940 KB Output is correct
59 Correct 77 ms 11556 KB Output is correct
60 Correct 76 ms 11396 KB Output is correct
61 Correct 73 ms 11836 KB Output is correct
62 Correct 76 ms 13012 KB Output is correct
63 Correct 78 ms 11884 KB Output is correct
64 Correct 77 ms 12824 KB Output is correct
65 Correct 77 ms 10896 KB Output is correct
66 Correct 77 ms 11328 KB Output is correct
67 Correct 79 ms 12300 KB Output is correct
68 Correct 78 ms 13020 KB Output is correct
69 Correct 324 ms 11788 KB Output is correct
70 Correct 356 ms 12084 KB Output is correct
71 Correct 351 ms 12812 KB Output is correct
72 Correct 274 ms 11812 KB Output is correct
73 Correct 283 ms 12876 KB Output is correct
74 Correct 106 ms 12352 KB Output is correct
75 Correct 126 ms 13784 KB Output is correct
76 Correct 276 ms 12620 KB Output is correct
77 Correct 312 ms 13360 KB Output is correct
78 Correct 309 ms 14068 KB Output is correct
79 Correct 170 ms 11688 KB Output is correct
80 Correct 324 ms 12632 KB Output is correct
81 Correct 348 ms 13488 KB Output is correct
82 Correct 348 ms 14036 KB Output is correct
83 Correct 496 ms 5424 KB Output is correct
84 Correct 1196 ms 6824 KB Output is correct
85 Correct 620 ms 6556 KB Output is correct
86 Correct 468 ms 5432 KB Output is correct
87 Correct 572 ms 5424 KB Output is correct
88 Correct 615 ms 6304 KB Output is correct
89 Correct 546 ms 5440 KB Output is correct
90 Correct 565 ms 6548 KB Output is correct
91 Correct 376 ms 4684 KB Output is correct
92 Correct 595 ms 5692 KB Output is correct
93 Correct 600 ms 6564 KB Output is correct
94 Correct 1175 ms 13784 KB Output is correct
95 Execution timed out 2055 ms 13608 KB Time limit exceeded
96 Halted 0 ms 0 KB -