답안 #765581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765581 2023-06-24T21:15:42 Z oyber Joker (BOI20_joker) C++14
49 / 100
2000 ms 9736 KB
#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 parity(int n) {
    if (u[n] == n) return 0;
    return parity(u[n])^p[n];
}

struct UniteInfo {
    int a;
    int b;
    int sa;
};

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,
        s[ap] - s[bp],
    };
}

void undo(UniteInfo info) {
    u[info.b] = info.b;
    s[info.a] = info.sa;
    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 = 256;
    int group_num = (q[q.size()-1].l+group_size) / group_size;
    //printf("%d %d\n", int(q[q.size()-1].l), group_num);
    vector<vector<Query>> groups(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]);
            //printf("g: %d q: %d\n", i, q[j].i);
            j++;
        }
        sort(groups[i].begin(), groups[i].end(), comp_query);
    }

    stack<UniteInfo> infos;
    vector<bool> result(qn);
    for (int j = 0; j < group_num; j++) {
        //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 < j*group_size; current_l++) {
            int a = e[current_l].first;
            int b = e[current_l].second;
            //printf("%d %d %d\n", current_l, a, b);
            if (find(a) == find(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);
                if (find(a) == find(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(unite(a, b));
                }
            }
            if (cycle) {
                result[groups[j][i].i] = true;
            }
            while (infos.size() > 0) {
                undo(infos.top());
                infos.pop();
            }
        }
    }

    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:71:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     scanf("%d %d %d", &n, &m, &qn);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
Joker.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 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 0 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
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 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 0 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 5 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 340 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 82 ms 8524 KB Output is correct
4 Correct 81 ms 9664 KB Output is correct
5 Correct 75 ms 9492 KB Output is correct
6 Correct 75 ms 8520 KB Output is correct
7 Correct 75 ms 8552 KB Output is correct
8 Correct 75 ms 8152 KB Output is correct
9 Correct 74 ms 8624 KB Output is correct
10 Correct 76 ms 9708 KB Output is correct
11 Correct 77 ms 8532 KB Output is correct
12 Correct 80 ms 9408 KB Output is correct
13 Correct 81 ms 7616 KB Output is correct
14 Correct 88 ms 8092 KB Output is correct
15 Correct 76 ms 9028 KB Output is correct
16 Correct 77 ms 9704 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 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 0 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 82 ms 8524 KB Output is correct
30 Correct 81 ms 9664 KB Output is correct
31 Correct 75 ms 9492 KB Output is correct
32 Correct 75 ms 8520 KB Output is correct
33 Correct 75 ms 8552 KB Output is correct
34 Correct 75 ms 8152 KB Output is correct
35 Correct 74 ms 8624 KB Output is correct
36 Correct 76 ms 9708 KB Output is correct
37 Correct 77 ms 8532 KB Output is correct
38 Correct 80 ms 9408 KB Output is correct
39 Correct 81 ms 7616 KB Output is correct
40 Correct 88 ms 8092 KB Output is correct
41 Correct 76 ms 9028 KB Output is correct
42 Correct 77 ms 9704 KB Output is correct
43 Correct 383 ms 8560 KB Output is correct
44 Correct 433 ms 9664 KB Output is correct
45 Correct 426 ms 9664 KB Output is correct
46 Correct 328 ms 8552 KB Output is correct
47 Correct 356 ms 8512 KB Output is correct
48 Correct 117 ms 8528 KB Output is correct
49 Correct 167 ms 9708 KB Output is correct
50 Correct 325 ms 8332 KB Output is correct
51 Correct 356 ms 9028 KB Output is correct
52 Correct 373 ms 9736 KB Output is correct
53 Correct 225 ms 7616 KB Output is correct
54 Correct 424 ms 8328 KB Output is correct
55 Correct 432 ms 8992 KB Output is correct
56 Correct 478 ms 9732 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 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 0 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 5 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 340 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 ms 340 KB Output is correct
55 Correct 1801 ms 3104 KB Output is correct
56 Execution timed out 2089 ms 4216 KB Time limit exceeded
57 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 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 0 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 5 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 340 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 ms 340 KB Output is correct
55 Correct 82 ms 8524 KB Output is correct
56 Correct 81 ms 9664 KB Output is correct
57 Correct 75 ms 9492 KB Output is correct
58 Correct 75 ms 8520 KB Output is correct
59 Correct 75 ms 8552 KB Output is correct
60 Correct 75 ms 8152 KB Output is correct
61 Correct 74 ms 8624 KB Output is correct
62 Correct 76 ms 9708 KB Output is correct
63 Correct 77 ms 8532 KB Output is correct
64 Correct 80 ms 9408 KB Output is correct
65 Correct 81 ms 7616 KB Output is correct
66 Correct 88 ms 8092 KB Output is correct
67 Correct 76 ms 9028 KB Output is correct
68 Correct 77 ms 9704 KB Output is correct
69 Correct 383 ms 8560 KB Output is correct
70 Correct 433 ms 9664 KB Output is correct
71 Correct 426 ms 9664 KB Output is correct
72 Correct 328 ms 8552 KB Output is correct
73 Correct 356 ms 8512 KB Output is correct
74 Correct 117 ms 8528 KB Output is correct
75 Correct 167 ms 9708 KB Output is correct
76 Correct 325 ms 8332 KB Output is correct
77 Correct 356 ms 9028 KB Output is correct
78 Correct 373 ms 9736 KB Output is correct
79 Correct 225 ms 7616 KB Output is correct
80 Correct 424 ms 8328 KB Output is correct
81 Correct 432 ms 8992 KB Output is correct
82 Correct 478 ms 9732 KB Output is correct
83 Correct 1801 ms 3104 KB Output is correct
84 Execution timed out 2089 ms 4216 KB Time limit exceeded
85 Halted 0 ms 0 KB -