답안 #527985

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527985 2022-02-18T22:26:51 Z Olympia 다리 (APIO19_bridges) C++17
100 / 100
2653 ms 27368 KB
#include <cmath>
#include <cassert>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#include <stack>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimization ("Ofast")

using namespace std;

class Updates {
public:
    int parentInd;
    int prev_parent;
    int compInd;
    int prev_comp;
};

class DisjointSetUnion {
private:
    vector<int> parent;
    vector<int> compSize;
    int n;
    int connectedComponents;
    stack<Updates> myStack;
public:
    int cntr = 0;
    int getConnectedComponents() const {
        return connectedComponents;
    }

    int getNeighbors(int x) {
        return compSize[find_head(x)];
    }

public:
    void resz(int sz) {
        n = sz;
        connectedComponents = sz;
        parent.resize(sz), compSize.resize(sz);
        for (int i = 0; i < n; i++) {
            parent[i] = i, compSize[i] = 1;
        }
    }

    int find_head(int x) const {
        int cur = x;
        while (cur != parent[cur]) {
            cur = parent[cur];
        }
        return cur;
    }

    void join(int x, int y, bool b) {
        x = find_head(x);
        y = find_head(y);
        if (x == y) {
            return;
        }
        if (compSize[x] > compSize[y]) {
            swap(x, y);
        }
        if (b) myStack.push({x, parent[x], y, compSize[y]}), cntr++;
        parent[x] = y;
        compSize[y] += compSize[x];
        connectedComponents--;
    }

    void revert(int x) {
        while (x--) {
            Updates u = myStack.top();
            myStack.pop();
            parent[u.parentInd] = u.prev_parent;
            compSize[u.compInd] = u.prev_comp;
            connectedComponents++;
        }
    }

    bool comp(int x, int y) {
        return (find_head(x) == find_head(y));
    }
};

class Query {
public:
    int w;
    int x;
    int type;
    int q;

    Query(int w, int x, int type, int q) {
        this->w = w, this->x = x, this->type = type, this->q = q;
    }

    bool operator<(const Query myQuery) const {
        if (myQuery.w != w) return (myQuery.w < w);
        if (myQuery.x != x) return (myQuery.x < x);
        if (myQuery.type != type) return (myQuery.type < type);
        return false;
    }
};

bool comp_index(Query a, Query b) {
    return (a.q < b.q);
}

void compress(vector<pair<pair<int, int>, int>> &edges, vector<vector<Query>> &blocks) {
    set<int> w;
    for (auto &p: edges) w.insert(p.second);
    for (auto &v: blocks) for (auto &q: v) w.insert(q.w);
    map<int, int> myMap;
    int cntr = 0;
    for (int i: w) {
        myMap[i] = cntr++;
    }
    for (int i = 0; i < edges.size(); i++) {
        edges[i].second = myMap[edges[i].second];
    }
    for (int i = 0; i < blocks.size(); i++) {
        for (int j = 0; j < blocks[i].size(); j++) {
            blocks[i][j].w = myMap[blocks[i][j].w];
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, M;
    cin >> N >> M;
    vector<pair<pair<int, int>, int>> edges;
    for (int i = 0; i < M; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        x--, y--;
        edges.push_back({{x, y}, z});
    }
    int Q;
    cin >> Q;
    vector<vector<Query>> blocks;
    blocks.emplace_back();
    for (int i = 0; i < Q; i++) {
        if (blocks.back().size() > 1000) {
            blocks.emplace_back();
        }
        int t, w, x;
        cin >> t >> x >> w;
        x--;
        blocks.back().emplace_back(Query(w, x, t, i));
    }
    compress(edges, blocks);
    vector<int> default_edges(M);
    for (int i = 0; i < M; i++) {
        default_edges[i] = edges[i].second;
    }
    vector<pair<int, int>> myVec;
    int total = 0;
    for (auto &v: blocks) {
        DisjointSetUnion dsu;
        dsu.resz(N);
        vector<int> uncertain;
        int cnt[M];
        for (int i = 0; i < M; i++) {
            cnt[i] = 0;
        }
        for (Query& q: v) {
            if (q.type == 1) {
                uncertain.push_back(q.x);
                cnt[q.x]++;
            }
        }
        stack<pair<int,int>> myStack;
        vector<int> mnt[v.size()];
        for (int i = 0; i < v.size(); i++) {
            if (v[i].type == 1) {
                default_edges[v[i].x] = v[i].w;
                myStack.push({v[i].x, v[i].w});
            } else {
                for (int j: uncertain) {
                    if (default_edges[j] >= v[i].w) {
                        mnt[i].push_back(j);
                    }
                }
            }
        }
        while (!myStack.empty()) {
            default_edges[myStack.top().first] = myStack.top().second;
            myStack.pop();
        }
        vector<int> e[M + Q];
        for (int i = 0; i < M; i++) {
            if (!cnt[i]) {
                e[default_edges[i]].push_back(i);
            }
        }
        sort (v.begin(), v.end());
        int prev = M + Q;
        for (int ind = 0; ind < v.size(); ind++) {
            if (v[ind].type == 1) {
                continue;
            }
            Query q = v[ind];
            for (int i = q.w; i < prev; i++) {
                for (int j: e[i]) {
                    dsu.join(edges[j].first.first, edges[j].first.second, 0);
                }
            }
            prev = q.w;
            dsu.cntr = 0;
            for (auto& x: mnt[v[ind].q - total]) {
                dsu.join(edges[x].first.first, edges[x].first.second, 1);
            }
            myVec.emplace_back(q.q, dsu.getNeighbors(q.x));
            dsu.revert(dsu.cntr);
        }
        sort(v.begin(), v.end(), comp_index);
        for (Query &q: v) {
            if (q.type == 1) {
                default_edges[q.x] = q.w;
            }
        }
        total += v.size();
    }

    sort(myVec.begin(), myVec.end());
    for (auto &p: myVec) {
        cout << p.second << '\n';
    }
}

Compilation message

bridges.cpp:15: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   15 | #pragma GCC optimization ("O3")
      | 
bridges.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   16 | #pragma GCC optimization ("unroll-loops")
      | 
bridges.cpp:17: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   17 | #pragma GCC optimization ("Ofast")
      | 
bridges.cpp: In function 'void compress(std::vector<std::pair<std::pair<int, int>, int> >&, std::vector<std::vector<Query> >&)':
bridges.cpp:126:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
bridges.cpp:129:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<Query> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for (int i = 0; i < blocks.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
bridges.cpp:130:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |         for (int j = 0; j < blocks[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:184:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |         for (int i = 0; i < v.size(); i++) {
      |                         ~~^~~~~~~~~~
bridges.cpp:208:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  208 |         for (int ind = 0; ind < v.size(); ind++) {
      |                           ~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 37 ms 1740 KB Output is correct
4 Correct 10 ms 1612 KB Output is correct
5 Correct 37 ms 1688 KB Output is correct
6 Correct 26 ms 1740 KB Output is correct
7 Correct 32 ms 1976 KB Output is correct
8 Correct 41 ms 1572 KB Output is correct
9 Correct 43 ms 2512 KB Output is correct
10 Correct 51 ms 1652 KB Output is correct
11 Correct 44 ms 1664 KB Output is correct
12 Correct 49 ms 1836 KB Output is correct
13 Correct 46 ms 1660 KB Output is correct
14 Correct 46 ms 1656 KB Output is correct
15 Correct 47 ms 1672 KB Output is correct
16 Correct 39 ms 2168 KB Output is correct
17 Correct 49 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1711 ms 20328 KB Output is correct
2 Correct 1707 ms 20284 KB Output is correct
3 Correct 1754 ms 20432 KB Output is correct
4 Correct 1996 ms 20332 KB Output is correct
5 Correct 1825 ms 20236 KB Output is correct
6 Correct 2419 ms 18660 KB Output is correct
7 Correct 2436 ms 19380 KB Output is correct
8 Correct 2371 ms 20048 KB Output is correct
9 Correct 165 ms 13792 KB Output is correct
10 Correct 1056 ms 9516 KB Output is correct
11 Correct 993 ms 9096 KB Output is correct
12 Correct 1467 ms 19084 KB Output is correct
13 Correct 1553 ms 20416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1408 ms 18040 KB Output is correct
2 Correct 1008 ms 12528 KB Output is correct
3 Correct 1551 ms 15668 KB Output is correct
4 Correct 1362 ms 18044 KB Output is correct
5 Correct 153 ms 13796 KB Output is correct
6 Correct 1611 ms 18160 KB Output is correct
7 Correct 1283 ms 18164 KB Output is correct
8 Correct 1099 ms 18088 KB Output is correct
9 Correct 998 ms 18288 KB Output is correct
10 Correct 938 ms 17960 KB Output is correct
11 Correct 1027 ms 18028 KB Output is correct
12 Correct 896 ms 18032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2368 ms 27368 KB Output is correct
2 Correct 160 ms 13788 KB Output is correct
3 Correct 262 ms 14884 KB Output is correct
4 Correct 68 ms 6088 KB Output is correct
5 Correct 580 ms 11080 KB Output is correct
6 Correct 2286 ms 27180 KB Output is correct
7 Correct 538 ms 11044 KB Output is correct
8 Correct 1174 ms 20444 KB Output is correct
9 Correct 1111 ms 20464 KB Output is correct
10 Correct 976 ms 20440 KB Output is correct
11 Correct 1773 ms 23896 KB Output is correct
12 Correct 1725 ms 23908 KB Output is correct
13 Correct 1521 ms 23940 KB Output is correct
14 Correct 532 ms 11172 KB Output is correct
15 Correct 540 ms 11320 KB Output is correct
16 Correct 2401 ms 27316 KB Output is correct
17 Correct 2221 ms 27232 KB Output is correct
18 Correct 2296 ms 27292 KB Output is correct
19 Correct 2167 ms 27260 KB Output is correct
20 Correct 1591 ms 24920 KB Output is correct
21 Correct 1602 ms 24748 KB Output is correct
22 Correct 1906 ms 26836 KB Output is correct
23 Correct 517 ms 10988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1711 ms 20328 KB Output is correct
2 Correct 1707 ms 20284 KB Output is correct
3 Correct 1754 ms 20432 KB Output is correct
4 Correct 1996 ms 20332 KB Output is correct
5 Correct 1825 ms 20236 KB Output is correct
6 Correct 2419 ms 18660 KB Output is correct
7 Correct 2436 ms 19380 KB Output is correct
8 Correct 2371 ms 20048 KB Output is correct
9 Correct 165 ms 13792 KB Output is correct
10 Correct 1056 ms 9516 KB Output is correct
11 Correct 993 ms 9096 KB Output is correct
12 Correct 1467 ms 19084 KB Output is correct
13 Correct 1553 ms 20416 KB Output is correct
14 Correct 1408 ms 18040 KB Output is correct
15 Correct 1008 ms 12528 KB Output is correct
16 Correct 1551 ms 15668 KB Output is correct
17 Correct 1362 ms 18044 KB Output is correct
18 Correct 153 ms 13796 KB Output is correct
19 Correct 1611 ms 18160 KB Output is correct
20 Correct 1283 ms 18164 KB Output is correct
21 Correct 1099 ms 18088 KB Output is correct
22 Correct 998 ms 18288 KB Output is correct
23 Correct 938 ms 17960 KB Output is correct
24 Correct 1027 ms 18028 KB Output is correct
25 Correct 896 ms 18032 KB Output is correct
26 Correct 1725 ms 20268 KB Output is correct
27 Correct 1992 ms 17968 KB Output is correct
28 Correct 1770 ms 20276 KB Output is correct
29 Correct 1284 ms 20304 KB Output is correct
30 Correct 1931 ms 19260 KB Output is correct
31 Correct 1983 ms 18924 KB Output is correct
32 Correct 1969 ms 18884 KB Output is correct
33 Correct 1751 ms 20328 KB Output is correct
34 Correct 1711 ms 20316 KB Output is correct
35 Correct 1723 ms 20412 KB Output is correct
36 Correct 1363 ms 20280 KB Output is correct
37 Correct 1362 ms 20228 KB Output is correct
38 Correct 1325 ms 20280 KB Output is correct
39 Correct 1144 ms 20412 KB Output is correct
40 Correct 1135 ms 20292 KB Output is correct
41 Correct 1158 ms 20304 KB Output is correct
42 Correct 1144 ms 20348 KB Output is correct
43 Correct 1106 ms 20284 KB Output is correct
44 Correct 1109 ms 20200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 37 ms 1740 KB Output is correct
4 Correct 10 ms 1612 KB Output is correct
5 Correct 37 ms 1688 KB Output is correct
6 Correct 26 ms 1740 KB Output is correct
7 Correct 32 ms 1976 KB Output is correct
8 Correct 41 ms 1572 KB Output is correct
9 Correct 43 ms 2512 KB Output is correct
10 Correct 51 ms 1652 KB Output is correct
11 Correct 44 ms 1664 KB Output is correct
12 Correct 49 ms 1836 KB Output is correct
13 Correct 46 ms 1660 KB Output is correct
14 Correct 46 ms 1656 KB Output is correct
15 Correct 47 ms 1672 KB Output is correct
16 Correct 39 ms 2168 KB Output is correct
17 Correct 49 ms 1912 KB Output is correct
18 Correct 1711 ms 20328 KB Output is correct
19 Correct 1707 ms 20284 KB Output is correct
20 Correct 1754 ms 20432 KB Output is correct
21 Correct 1996 ms 20332 KB Output is correct
22 Correct 1825 ms 20236 KB Output is correct
23 Correct 2419 ms 18660 KB Output is correct
24 Correct 2436 ms 19380 KB Output is correct
25 Correct 2371 ms 20048 KB Output is correct
26 Correct 165 ms 13792 KB Output is correct
27 Correct 1056 ms 9516 KB Output is correct
28 Correct 993 ms 9096 KB Output is correct
29 Correct 1467 ms 19084 KB Output is correct
30 Correct 1553 ms 20416 KB Output is correct
31 Correct 1408 ms 18040 KB Output is correct
32 Correct 1008 ms 12528 KB Output is correct
33 Correct 1551 ms 15668 KB Output is correct
34 Correct 1362 ms 18044 KB Output is correct
35 Correct 153 ms 13796 KB Output is correct
36 Correct 1611 ms 18160 KB Output is correct
37 Correct 1283 ms 18164 KB Output is correct
38 Correct 1099 ms 18088 KB Output is correct
39 Correct 998 ms 18288 KB Output is correct
40 Correct 938 ms 17960 KB Output is correct
41 Correct 1027 ms 18028 KB Output is correct
42 Correct 896 ms 18032 KB Output is correct
43 Correct 2368 ms 27368 KB Output is correct
44 Correct 160 ms 13788 KB Output is correct
45 Correct 262 ms 14884 KB Output is correct
46 Correct 68 ms 6088 KB Output is correct
47 Correct 580 ms 11080 KB Output is correct
48 Correct 2286 ms 27180 KB Output is correct
49 Correct 538 ms 11044 KB Output is correct
50 Correct 1174 ms 20444 KB Output is correct
51 Correct 1111 ms 20464 KB Output is correct
52 Correct 976 ms 20440 KB Output is correct
53 Correct 1773 ms 23896 KB Output is correct
54 Correct 1725 ms 23908 KB Output is correct
55 Correct 1521 ms 23940 KB Output is correct
56 Correct 532 ms 11172 KB Output is correct
57 Correct 540 ms 11320 KB Output is correct
58 Correct 2401 ms 27316 KB Output is correct
59 Correct 2221 ms 27232 KB Output is correct
60 Correct 2296 ms 27292 KB Output is correct
61 Correct 2167 ms 27260 KB Output is correct
62 Correct 1591 ms 24920 KB Output is correct
63 Correct 1602 ms 24748 KB Output is correct
64 Correct 1906 ms 26836 KB Output is correct
65 Correct 517 ms 10988 KB Output is correct
66 Correct 1725 ms 20268 KB Output is correct
67 Correct 1992 ms 17968 KB Output is correct
68 Correct 1770 ms 20276 KB Output is correct
69 Correct 1284 ms 20304 KB Output is correct
70 Correct 1931 ms 19260 KB Output is correct
71 Correct 1983 ms 18924 KB Output is correct
72 Correct 1969 ms 18884 KB Output is correct
73 Correct 1751 ms 20328 KB Output is correct
74 Correct 1711 ms 20316 KB Output is correct
75 Correct 1723 ms 20412 KB Output is correct
76 Correct 1363 ms 20280 KB Output is correct
77 Correct 1362 ms 20228 KB Output is correct
78 Correct 1325 ms 20280 KB Output is correct
79 Correct 1144 ms 20412 KB Output is correct
80 Correct 1135 ms 20292 KB Output is correct
81 Correct 1158 ms 20304 KB Output is correct
82 Correct 1144 ms 20348 KB Output is correct
83 Correct 1106 ms 20284 KB Output is correct
84 Correct 1109 ms 20200 KB Output is correct
85 Correct 2602 ms 26932 KB Output is correct
86 Correct 265 ms 14924 KB Output is correct
87 Correct 99 ms 7832 KB Output is correct
88 Correct 926 ms 11632 KB Output is correct
89 Correct 2599 ms 26888 KB Output is correct
90 Correct 936 ms 11752 KB Output is correct
91 Correct 1758 ms 20276 KB Output is correct
92 Correct 1738 ms 20336 KB Output is correct
93 Correct 2384 ms 20452 KB Output is correct
94 Correct 2079 ms 23576 KB Output is correct
95 Correct 2143 ms 23728 KB Output is correct
96 Correct 2189 ms 23840 KB Output is correct
97 Correct 663 ms 11572 KB Output is correct
98 Correct 703 ms 10256 KB Output is correct
99 Correct 2568 ms 26812 KB Output is correct
100 Correct 2539 ms 26932 KB Output is correct
101 Correct 2653 ms 26804 KB Output is correct
102 Correct 2623 ms 26796 KB Output is correct
103 Correct 2438 ms 24928 KB Output is correct
104 Correct 2406 ms 24872 KB Output is correct
105 Correct 2142 ms 24756 KB Output is correct
106 Correct 1742 ms 23652 KB Output is correct
107 Correct 1844 ms 24960 KB Output is correct
108 Correct 2536 ms 26576 KB Output is correct
109 Correct 1042 ms 10820 KB Output is correct