답안 #860727

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
860727 2023-10-14T04:17:10 Z E869120 Stranded Far From Home (BOI22_island) C++14
100 / 100
203 ms 62688 KB
#include <bits/stdc++.h>
using namespace std;

class UnionFind {
    public:
    vector<long long> par;
    vector<long long> sum;

    void init(int sz) {
        par.resize(sz, -1);
        sum.resize(sz, 0);
    }

    void touroku(int pos, long long x) {
        sum[pos] += x;
    }

    int root(int pos) {
        if (par[pos] == -1) return pos;
        par[pos] = root(par[pos]);
        return par[pos];
    }

    void unite(int u, int v) {
        u = root(u); v = root(v);
        if (u == v) return;
        if (u > v) swap(u, v);
        sum[v] += sum[u];
        par[u] = v; // Add edge v -> u
    }

    bool same(int u, int v) {
        if (root(u) == root(v)) return true;
        return false;
    }
};

long long N, S[1 << 19];
long long M, A[1 << 19], B[1 << 19];
bool Eval[1 << 19];
vector<long long> Zahyou;
vector<pair<int, int>> Edge[1 << 19];
vector<int> G[1 << 19];
UnionFind UF;

void dfs(int pos) {
    for (int to : G[pos]) Eval[to] |= Eval[pos];
    for (int to : G[pos]) dfs(to);
}

int main() {
    // Step 1. Input
    scanf("%lld%lld", &N, &M);
    for (int i = 1; i <= N; i++) scanf("%lld", &S[i]);
    for (int i = 1; i <= M; i++) scanf("%lld%lld", &A[i], &B[i]);

    // Step 2. Compression
    for (int i = 1; i <= N; i++) Zahyou.push_back(S[i]);
    sort(Zahyou.begin(), Zahyou.end());
    Zahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());
    
    // Step 3. Add Edges
    for (int i = 1; i <= M; i++) {
        int pos1 = lower_bound(Zahyou.begin(), Zahyou.end(), max(S[A[i]], S[B[i]])) - Zahyou.begin();
        Edge[pos1].push_back(make_pair(A[i], B[i]));
    }

    // Step 4. Union Find
    UF.init(2 * N + 2);
    for (int i = 1; i <= N; i++) UF.touroku(i, S[i]);
    int cnts = N;
    for (int i = 0; i < Zahyou.size(); i++) {
        for (pair<int, int> e : Edge[i]) {
            int idx1 = UF.root(e.first);
            int idx2 = UF.root(e.second);
            if (UF.sum[idx1] < Zahyou[i]) Eval[idx1] = true;
            if (UF.sum[idx2] < Zahyou[i]) Eval[idx2] = true;
        }
        for (pair<int, int> e : Edge[i]) {
            int idx1 = UF.root(e.first);
            int idx2 = UF.root(e.second);
            if (UF.same(idx1, idx2) == true) continue;
            cnts += 1;
            if (UF.same(cnts, idx1) == false) { UF.unite(cnts, idx1); G[cnts].push_back(idx1); }
            if (UF.same(cnts, idx2) == false) { UF.unite(cnts, idx2); G[cnts].push_back(idx2); }
        }
    }

    // Step 5. DFS
    dfs(cnts);
    for (int i = 1; i <= N; i++) {
        if (Eval[i] == false) printf("1");
        else printf("0");
    }
    printf("\n");
    return 0;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |     for (int i = 0; i < Zahyou.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
island.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     scanf("%lld%lld", &N, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
island.cpp:54:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |     for (int i = 1; i <= N; i++) scanf("%lld", &S[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~
island.cpp:55:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     for (int i = 1; i <= M; i++) scanf("%lld%lld", &A[i], &B[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 8 ms 31580 KB Output is correct
6 Correct 7 ms 31644 KB Output is correct
7 Correct 7 ms 31580 KB Output is correct
8 Correct 7 ms 31324 KB Output is correct
9 Correct 7 ms 31636 KB Output is correct
10 Correct 8 ms 31580 KB Output is correct
11 Correct 7 ms 31580 KB Output is correct
12 Correct 7 ms 31580 KB Output is correct
13 Correct 7 ms 31324 KB Output is correct
14 Correct 7 ms 31388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB Output is correct
2 Correct 5 ms 27480 KB Output is correct
3 Correct 132 ms 53332 KB Output is correct
4 Correct 92 ms 47108 KB Output is correct
5 Correct 156 ms 49340 KB Output is correct
6 Correct 135 ms 49344 KB Output is correct
7 Correct 145 ms 49760 KB Output is correct
8 Correct 101 ms 47132 KB Output is correct
9 Correct 114 ms 48268 KB Output is correct
10 Correct 94 ms 53428 KB Output is correct
11 Correct 84 ms 54428 KB Output is correct
12 Correct 98 ms 47296 KB Output is correct
13 Correct 90 ms 47040 KB Output is correct
14 Correct 96 ms 47096 KB Output is correct
15 Correct 132 ms 58044 KB Output is correct
16 Correct 90 ms 57844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27224 KB Output is correct
2 Correct 158 ms 49644 KB Output is correct
3 Correct 145 ms 49152 KB Output is correct
4 Correct 94 ms 47052 KB Output is correct
5 Correct 88 ms 47228 KB Output is correct
6 Correct 175 ms 49600 KB Output is correct
7 Correct 166 ms 58160 KB Output is correct
8 Correct 154 ms 58048 KB Output is correct
9 Correct 94 ms 58096 KB Output is correct
10 Correct 100 ms 47216 KB Output is correct
11 Correct 100 ms 47552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27228 KB Output is correct
2 Correct 113 ms 47624 KB Output is correct
3 Correct 126 ms 51800 KB Output is correct
4 Correct 128 ms 51868 KB Output is correct
5 Correct 101 ms 51680 KB Output is correct
6 Correct 100 ms 51304 KB Output is correct
7 Correct 93 ms 57984 KB Output is correct
8 Correct 80 ms 56696 KB Output is correct
9 Correct 73 ms 43204 KB Output is correct
10 Correct 106 ms 50136 KB Output is correct
11 Correct 97 ms 50284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 8 ms 31580 KB Output is correct
6 Correct 7 ms 31644 KB Output is correct
7 Correct 7 ms 31580 KB Output is correct
8 Correct 7 ms 31324 KB Output is correct
9 Correct 7 ms 31636 KB Output is correct
10 Correct 8 ms 31580 KB Output is correct
11 Correct 7 ms 31580 KB Output is correct
12 Correct 7 ms 31580 KB Output is correct
13 Correct 7 ms 31324 KB Output is correct
14 Correct 7 ms 31388 KB Output is correct
15 Correct 6 ms 31320 KB Output is correct
16 Correct 5 ms 27480 KB Output is correct
17 Correct 132 ms 53332 KB Output is correct
18 Correct 92 ms 47108 KB Output is correct
19 Correct 156 ms 49340 KB Output is correct
20 Correct 135 ms 49344 KB Output is correct
21 Correct 145 ms 49760 KB Output is correct
22 Correct 101 ms 47132 KB Output is correct
23 Correct 114 ms 48268 KB Output is correct
24 Correct 94 ms 53428 KB Output is correct
25 Correct 84 ms 54428 KB Output is correct
26 Correct 98 ms 47296 KB Output is correct
27 Correct 90 ms 47040 KB Output is correct
28 Correct 96 ms 47096 KB Output is correct
29 Correct 132 ms 58044 KB Output is correct
30 Correct 90 ms 57844 KB Output is correct
31 Correct 5 ms 27224 KB Output is correct
32 Correct 158 ms 49644 KB Output is correct
33 Correct 145 ms 49152 KB Output is correct
34 Correct 94 ms 47052 KB Output is correct
35 Correct 88 ms 47228 KB Output is correct
36 Correct 175 ms 49600 KB Output is correct
37 Correct 166 ms 58160 KB Output is correct
38 Correct 154 ms 58048 KB Output is correct
39 Correct 94 ms 58096 KB Output is correct
40 Correct 100 ms 47216 KB Output is correct
41 Correct 100 ms 47552 KB Output is correct
42 Correct 5 ms 27228 KB Output is correct
43 Correct 113 ms 47624 KB Output is correct
44 Correct 126 ms 51800 KB Output is correct
45 Correct 128 ms 51868 KB Output is correct
46 Correct 101 ms 51680 KB Output is correct
47 Correct 100 ms 51304 KB Output is correct
48 Correct 93 ms 57984 KB Output is correct
49 Correct 80 ms 56696 KB Output is correct
50 Correct 73 ms 43204 KB Output is correct
51 Correct 106 ms 50136 KB Output is correct
52 Correct 97 ms 50284 KB Output is correct
53 Correct 6 ms 31324 KB Output is correct
54 Correct 6 ms 31320 KB Output is correct
55 Correct 6 ms 27228 KB Output is correct
56 Correct 7 ms 31576 KB Output is correct
57 Correct 8 ms 31776 KB Output is correct
58 Correct 7 ms 31408 KB Output is correct
59 Correct 7 ms 31600 KB Output is correct
60 Correct 7 ms 31324 KB Output is correct
61 Correct 7 ms 31580 KB Output is correct
62 Correct 9 ms 31580 KB Output is correct
63 Correct 8 ms 31832 KB Output is correct
64 Correct 7 ms 31580 KB Output is correct
65 Correct 7 ms 31324 KB Output is correct
66 Correct 8 ms 31580 KB Output is correct
67 Correct 142 ms 57700 KB Output is correct
68 Correct 93 ms 50368 KB Output is correct
69 Correct 145 ms 53352 KB Output is correct
70 Correct 137 ms 53696 KB Output is correct
71 Correct 193 ms 54116 KB Output is correct
72 Correct 113 ms 51548 KB Output is correct
73 Correct 119 ms 52592 KB Output is correct
74 Correct 94 ms 56892 KB Output is correct
75 Correct 91 ms 56796 KB Output is correct
76 Correct 99 ms 50364 KB Output is correct
77 Correct 97 ms 49968 KB Output is correct
78 Correct 96 ms 50284 KB Output is correct
79 Correct 135 ms 62548 KB Output is correct
80 Correct 94 ms 61416 KB Output is correct
81 Correct 166 ms 53948 KB Output is correct
82 Correct 147 ms 53748 KB Output is correct
83 Correct 95 ms 51520 KB Output is correct
84 Correct 95 ms 50136 KB Output is correct
85 Correct 203 ms 54204 KB Output is correct
86 Correct 157 ms 62632 KB Output is correct
87 Correct 160 ms 62688 KB Output is correct
88 Correct 99 ms 51008 KB Output is correct
89 Correct 101 ms 50372 KB Output is correct
90 Correct 121 ms 52192 KB Output is correct
91 Correct 121 ms 51812 KB Output is correct
92 Correct 115 ms 51908 KB Output is correct
93 Correct 102 ms 51520 KB Output is correct
94 Correct 102 ms 51296 KB Output is correct
95 Correct 92 ms 57668 KB Output is correct
96 Correct 81 ms 56724 KB Output is correct
97 Correct 72 ms 43204 KB Output is correct
98 Correct 109 ms 50020 KB Output is correct
99 Correct 97 ms 50112 KB Output is correct
100 Correct 52 ms 36180 KB Output is correct
101 Correct 161 ms 53728 KB Output is correct
102 Correct 90 ms 49212 KB Output is correct
103 Correct 147 ms 52980 KB Output is correct
104 Correct 182 ms 54716 KB Output is correct