답안 #960666

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960666 2024-04-10T20:44:50 Z MisterReaper 다리 (APIO19_bridges) C++17
100 / 100
1558 ms 124812 KB
#include <bits/stdc++.h>
using i64 = long long;

constexpr int maxN = 5E4 + 5;

std::vector<std::pair<int, int>> operations;
int par[maxN], sz[maxN];
void reset() {
    std::iota(par, par + maxN, 0);
    std::fill(sz, sz + maxN, 1);
    operations.clear();
}

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

bool unite(int u, int v) {
    u = get(u), v = get(v);
    if(u == v) {
        return false;
    }
    if(sz[u] > sz[v]) {
        std::swap(u, v);
    }
    par[u] = v;
    sz[v] += sz[u];
    operations.emplace_back(u, v);
    return true;
}

void rollback() {
    auto [u, v] = operations.back();
    operations.pop_back();
    par[u] = u;
    sz[v] -= sz[u];
}

int size(int a) {
    return sz[get(a)];
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    std::vector<bool> changed;
    std::vector<int> u(m), v(m), w(m);
    for(int i = 0; i < m; i++) {
        std::cin >> u[i] >> v[i] >> w[i];
        u[i]--, v[i]--;
    }

    int q;
    std::cin >> q;

    std::vector<int> t(q), x(q), y(q);
    for(int i = 0; i < q; i++) {
        std::cin >> t[i] >> x[i] >> y[i];
        x[i]--;
    }

    constexpr int B = 1000;

    std::vector<std::vector<int>> deck(q);
    std::vector<int> ans(q);
    for(int l = 0; l < q; l += B) {
        int r = std::min(q, l + B);
        changed.assign(m, false);
        reset();

        std::vector<int> ask;
        for(int i = l; i < r; i++) {
            if(t[i] == 1) {
                changed[x[i]] = true;
            } else {
                ask.emplace_back(i);
            }
        }

        std::vector<int> edges, able;
        for(int i = 0; i < m; i++) {
            if(!changed[i]) {
                edges.emplace_back(i);
            } else {
                able.emplace_back(i);
            }
        }

        for(int i = l; i < r; i++) {
            if(t[i] == 1) {
                w[x[i]] = y[i];
            } else {
                for(int j : able) {
                    if(y[i] <= w[j]) {
                        deck[i].emplace_back(j);
                    }
                }
            }
        }

        std::sort(edges.begin(), edges.end(), [&](int lhs, int rhs) -> bool {
            return w[lhs] > w[rhs];
        });
        std::sort(ask.begin(), ask.end(), [&](int lhs, int rhs) -> bool {
            return y[lhs] > y[rhs];
        });
        int p = 0;
        for(int i : ask) {
            while(p != edges.size() && w[edges[p]] >= y[i]) {
                unite(u[edges[p]], v[edges[p]]);
                p++;
            }
            int ps = operations.size();
            for(int j : deck[i]) {
                unite(u[j], v[j]);
            }
            ans[i] = size(x[i]);
            while(operations.size() != ps) {
                rollback();
            }
            deck[i].clear();
        }
    }

    for(int i = 0; i < q; i++) {
        if(t[i] == 2) {
            std::cout << ans[i] << '\n';
        }
    }

    return 0;
}

Compilation message

bridges.cpp: In function 'int main()':
bridges.cpp:116:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |             while(p != edges.size() && w[edges[p]] >= y[i]) {
      |                   ~~^~~~~~~~~~~~~~~
bridges.cpp:125:37: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  125 |             while(operations.size() != ps) {
      |                   ~~~~~~~~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 23 ms 6748 KB Output is correct
4 Correct 4 ms 1116 KB Output is correct
5 Correct 9 ms 2712 KB Output is correct
6 Correct 7 ms 2648 KB Output is correct
7 Correct 8 ms 2652 KB Output is correct
8 Correct 9 ms 2396 KB Output is correct
9 Correct 10 ms 3600 KB Output is correct
10 Correct 9 ms 2648 KB Output is correct
11 Correct 9 ms 2652 KB Output is correct
12 Correct 10 ms 3164 KB Output is correct
13 Correct 12 ms 3676 KB Output is correct
14 Correct 13 ms 3164 KB Output is correct
15 Correct 11 ms 2840 KB Output is correct
16 Correct 8 ms 2980 KB Output is correct
17 Correct 9 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 849 ms 72636 KB Output is correct
2 Correct 857 ms 72516 KB Output is correct
3 Correct 841 ms 73180 KB Output is correct
4 Correct 832 ms 72304 KB Output is correct
5 Correct 832 ms 73064 KB Output is correct
6 Correct 1033 ms 124812 KB Output is correct
7 Correct 1027 ms 124404 KB Output is correct
8 Correct 1010 ms 122408 KB Output is correct
9 Correct 30 ms 4948 KB Output is correct
10 Correct 535 ms 96244 KB Output is correct
11 Correct 548 ms 96536 KB Output is correct
12 Correct 736 ms 53004 KB Output is correct
13 Correct 701 ms 46232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 698 ms 72496 KB Output is correct
2 Correct 430 ms 90324 KB Output is correct
3 Correct 707 ms 97700 KB Output is correct
4 Correct 676 ms 71896 KB Output is correct
5 Correct 29 ms 4952 KB Output is correct
6 Correct 701 ms 91988 KB Output is correct
7 Correct 610 ms 64196 KB Output is correct
8 Correct 578 ms 48616 KB Output is correct
9 Correct 495 ms 40788 KB Output is correct
10 Correct 449 ms 28924 KB Output is correct
11 Correct 480 ms 37888 KB Output is correct
12 Correct 439 ms 27600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1242 ms 7820 KB Output is correct
2 Correct 30 ms 6224 KB Output is correct
3 Correct 127 ms 5172 KB Output is correct
4 Correct 57 ms 5304 KB Output is correct
5 Correct 597 ms 9548 KB Output is correct
6 Correct 1195 ms 11384 KB Output is correct
7 Correct 614 ms 9800 KB Output is correct
8 Correct 580 ms 9264 KB Output is correct
9 Correct 608 ms 9012 KB Output is correct
10 Correct 562 ms 8980 KB Output is correct
11 Correct 903 ms 10040 KB Output is correct
12 Correct 890 ms 10176 KB Output is correct
13 Correct 905 ms 10196 KB Output is correct
14 Correct 532 ms 9972 KB Output is correct
15 Correct 563 ms 9520 KB Output is correct
16 Correct 1258 ms 11268 KB Output is correct
17 Correct 1244 ms 11260 KB Output is correct
18 Correct 1231 ms 11392 KB Output is correct
19 Correct 1213 ms 11228 KB Output is correct
20 Correct 1004 ms 10168 KB Output is correct
21 Correct 1004 ms 10376 KB Output is correct
22 Correct 1176 ms 11012 KB Output is correct
23 Correct 665 ms 8976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 849 ms 72636 KB Output is correct
2 Correct 857 ms 72516 KB Output is correct
3 Correct 841 ms 73180 KB Output is correct
4 Correct 832 ms 72304 KB Output is correct
5 Correct 832 ms 73064 KB Output is correct
6 Correct 1033 ms 124812 KB Output is correct
7 Correct 1027 ms 124404 KB Output is correct
8 Correct 1010 ms 122408 KB Output is correct
9 Correct 30 ms 4948 KB Output is correct
10 Correct 535 ms 96244 KB Output is correct
11 Correct 548 ms 96536 KB Output is correct
12 Correct 736 ms 53004 KB Output is correct
13 Correct 701 ms 46232 KB Output is correct
14 Correct 698 ms 72496 KB Output is correct
15 Correct 430 ms 90324 KB Output is correct
16 Correct 707 ms 97700 KB Output is correct
17 Correct 676 ms 71896 KB Output is correct
18 Correct 29 ms 4952 KB Output is correct
19 Correct 701 ms 91988 KB Output is correct
20 Correct 610 ms 64196 KB Output is correct
21 Correct 578 ms 48616 KB Output is correct
22 Correct 495 ms 40788 KB Output is correct
23 Correct 449 ms 28924 KB Output is correct
24 Correct 480 ms 37888 KB Output is correct
25 Correct 439 ms 27600 KB Output is correct
26 Correct 923 ms 72976 KB Output is correct
27 Correct 1039 ms 99800 KB Output is correct
28 Correct 922 ms 71024 KB Output is correct
29 Correct 707 ms 28164 KB Output is correct
30 Correct 1033 ms 87372 KB Output is correct
31 Correct 1004 ms 89572 KB Output is correct
32 Correct 1022 ms 89892 KB Output is correct
33 Correct 913 ms 71372 KB Output is correct
34 Correct 927 ms 71456 KB Output is correct
35 Correct 942 ms 71664 KB Output is correct
36 Correct 760 ms 35208 KB Output is correct
37 Correct 733 ms 33908 KB Output is correct
38 Correct 728 ms 32316 KB Output is correct
39 Correct 651 ms 18340 KB Output is correct
40 Correct 631 ms 17704 KB Output is correct
41 Correct 639 ms 17072 KB Output is correct
42 Correct 615 ms 16812 KB Output is correct
43 Correct 606 ms 16428 KB Output is correct
44 Correct 615 ms 15728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 23 ms 6748 KB Output is correct
4 Correct 4 ms 1116 KB Output is correct
5 Correct 9 ms 2712 KB Output is correct
6 Correct 7 ms 2648 KB Output is correct
7 Correct 8 ms 2652 KB Output is correct
8 Correct 9 ms 2396 KB Output is correct
9 Correct 10 ms 3600 KB Output is correct
10 Correct 9 ms 2648 KB Output is correct
11 Correct 9 ms 2652 KB Output is correct
12 Correct 10 ms 3164 KB Output is correct
13 Correct 12 ms 3676 KB Output is correct
14 Correct 13 ms 3164 KB Output is correct
15 Correct 11 ms 2840 KB Output is correct
16 Correct 8 ms 2980 KB Output is correct
17 Correct 9 ms 3164 KB Output is correct
18 Correct 849 ms 72636 KB Output is correct
19 Correct 857 ms 72516 KB Output is correct
20 Correct 841 ms 73180 KB Output is correct
21 Correct 832 ms 72304 KB Output is correct
22 Correct 832 ms 73064 KB Output is correct
23 Correct 1033 ms 124812 KB Output is correct
24 Correct 1027 ms 124404 KB Output is correct
25 Correct 1010 ms 122408 KB Output is correct
26 Correct 30 ms 4948 KB Output is correct
27 Correct 535 ms 96244 KB Output is correct
28 Correct 548 ms 96536 KB Output is correct
29 Correct 736 ms 53004 KB Output is correct
30 Correct 701 ms 46232 KB Output is correct
31 Correct 698 ms 72496 KB Output is correct
32 Correct 430 ms 90324 KB Output is correct
33 Correct 707 ms 97700 KB Output is correct
34 Correct 676 ms 71896 KB Output is correct
35 Correct 29 ms 4952 KB Output is correct
36 Correct 701 ms 91988 KB Output is correct
37 Correct 610 ms 64196 KB Output is correct
38 Correct 578 ms 48616 KB Output is correct
39 Correct 495 ms 40788 KB Output is correct
40 Correct 449 ms 28924 KB Output is correct
41 Correct 480 ms 37888 KB Output is correct
42 Correct 439 ms 27600 KB Output is correct
43 Correct 1242 ms 7820 KB Output is correct
44 Correct 30 ms 6224 KB Output is correct
45 Correct 127 ms 5172 KB Output is correct
46 Correct 57 ms 5304 KB Output is correct
47 Correct 597 ms 9548 KB Output is correct
48 Correct 1195 ms 11384 KB Output is correct
49 Correct 614 ms 9800 KB Output is correct
50 Correct 580 ms 9264 KB Output is correct
51 Correct 608 ms 9012 KB Output is correct
52 Correct 562 ms 8980 KB Output is correct
53 Correct 903 ms 10040 KB Output is correct
54 Correct 890 ms 10176 KB Output is correct
55 Correct 905 ms 10196 KB Output is correct
56 Correct 532 ms 9972 KB Output is correct
57 Correct 563 ms 9520 KB Output is correct
58 Correct 1258 ms 11268 KB Output is correct
59 Correct 1244 ms 11260 KB Output is correct
60 Correct 1231 ms 11392 KB Output is correct
61 Correct 1213 ms 11228 KB Output is correct
62 Correct 1004 ms 10168 KB Output is correct
63 Correct 1004 ms 10376 KB Output is correct
64 Correct 1176 ms 11012 KB Output is correct
65 Correct 665 ms 8976 KB Output is correct
66 Correct 923 ms 72976 KB Output is correct
67 Correct 1039 ms 99800 KB Output is correct
68 Correct 922 ms 71024 KB Output is correct
69 Correct 707 ms 28164 KB Output is correct
70 Correct 1033 ms 87372 KB Output is correct
71 Correct 1004 ms 89572 KB Output is correct
72 Correct 1022 ms 89892 KB Output is correct
73 Correct 913 ms 71372 KB Output is correct
74 Correct 927 ms 71456 KB Output is correct
75 Correct 942 ms 71664 KB Output is correct
76 Correct 760 ms 35208 KB Output is correct
77 Correct 733 ms 33908 KB Output is correct
78 Correct 728 ms 32316 KB Output is correct
79 Correct 651 ms 18340 KB Output is correct
80 Correct 631 ms 17704 KB Output is correct
81 Correct 639 ms 17072 KB Output is correct
82 Correct 615 ms 16812 KB Output is correct
83 Correct 606 ms 16428 KB Output is correct
84 Correct 615 ms 15728 KB Output is correct
85 Correct 1543 ms 77488 KB Output is correct
86 Correct 157 ms 11364 KB Output is correct
87 Correct 84 ms 15824 KB Output is correct
88 Correct 849 ms 88828 KB Output is correct
89 Correct 1538 ms 76368 KB Output is correct
90 Correct 857 ms 96392 KB Output is correct
91 Correct 954 ms 75400 KB Output is correct
92 Correct 939 ms 75420 KB Output is correct
93 Correct 1130 ms 112540 KB Output is correct
94 Correct 1259 ms 76356 KB Output is correct
95 Correct 1246 ms 76300 KB Output is correct
96 Correct 1303 ms 115884 KB Output is correct
97 Correct 706 ms 42716 KB Output is correct
98 Correct 698 ms 39604 KB Output is correct
99 Correct 1558 ms 78440 KB Output is correct
100 Correct 1545 ms 77200 KB Output is correct
101 Correct 1532 ms 77604 KB Output is correct
102 Correct 1551 ms 77392 KB Output is correct
103 Correct 1399 ms 120344 KB Output is correct
104 Correct 1387 ms 120084 KB Output is correct
105 Correct 1154 ms 50016 KB Output is correct
106 Correct 979 ms 30376 KB Output is correct
107 Correct 1141 ms 56808 KB Output is correct
108 Correct 1507 ms 112660 KB Output is correct
109 Correct 993 ms 114988 KB Output is correct