Submission #580167

# Submission time Handle Problem Language Result Execution time Memory
580167 2022-06-20T16:29:38 Z Josia Stranded Far From Home (BOI22_island) C++14
100 / 100
855 ms 45460 KB
#include <bits/stdc++.h>

using namespace std;

#define int int64_t


struct unionfindElem {
    int chef;
    int amount;
    vector<int> active;
};

bool compare(unionfindElem A, unionfindElem B) {
    tuple<int, int> a = {A.chef, A.amount};
    tuple<int, int> b = {B.chef, B.amount};
    return a<b;
}



vector<unionfindElem> chefs;

void init(vector<pair<int, int>> vilages) {
    for (pair<int, int> i: vilages) {
        chefs.push_back({i.second, i.first, {i.second}});
    }
    sort(chefs.begin(), chefs.end(), compare);
}


int find(int x) {
    if (chefs[x].chef == x) return x;
    return chefs[x].chef = find(chefs[x].chef);
}


void Merge(vector<int>& a, vector<int>& b) {
    if (a.size() > b.size()) {
        swap(a, b);
    }
    for (int i: a) b.push_back(i);
}


void unite(int a, int b) {
    a = find(a); b = find(b);
    if (a == b) return;

    chefs[a].chef = b;
    chefs[b].amount += chefs[a].amount;
    Merge(chefs[a].active, chefs[b].active);
}



signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    int n; int m; cin >> n >> m;

    vector<pair<int, int>> vilages(n);
    vector<int> inhabitants(n);

    for (int i = 0; i<n; i++) {
        cin >> vilages[i].first;
        inhabitants[i] = vilages[i].first;
        vilages[i].second = i;
    }

    sort(vilages.begin(), vilages.end());

    vector<vector<int>> graph(n);

    for (int i = 0; i<m; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    init(vilages);


    vector<int> newcomers;
    for (auto pii: vilages) {
        int i = pii.second;
        newcomers.push_back(i);
        
        for (int j: graph[i]) {
            if (inhabitants[i] > chefs[find(j)].amount) chefs[find(j)].active.clear();
            if (inhabitants[i] >= inhabitants[j]) unite(i, j);
        }
        // for (auto chef: chefs) {
        //     for (int i: chef.active) cerr << i << " ";
        //     cerr << "\n";
        //     cerr << chef.chef << " " << chef.amount << "\n";
        // }
        // cerr << "\n";
    }

    vector<int> res(n);

    auto chef = chefs[find(0)];
    for (int i: chef.active) res[i] = 1;

    for (int i: res) cout << i;
    cout << "\n";


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 684 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 724 KB Output is correct
13 Correct 5 ms 596 KB Output is correct
14 Correct 3 ms 596 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 324 ms 38208 KB Output is correct
4 Correct 616 ms 38144 KB Output is correct
5 Correct 347 ms 40484 KB Output is correct
6 Correct 377 ms 41940 KB Output is correct
7 Correct 391 ms 42028 KB Output is correct
8 Correct 855 ms 39344 KB Output is correct
9 Correct 379 ms 45460 KB Output is correct
10 Correct 303 ms 35432 KB Output is correct
11 Correct 728 ms 40220 KB Output is correct
12 Correct 686 ms 37168 KB Output is correct
13 Correct 781 ms 38140 KB Output is correct
14 Correct 556 ms 37752 KB Output is correct
15 Correct 331 ms 38524 KB Output is correct
16 Correct 230 ms 38072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 441 ms 42256 KB Output is correct
3 Correct 485 ms 41752 KB Output is correct
4 Correct 682 ms 39664 KB Output is correct
5 Correct 454 ms 37228 KB Output is correct
6 Correct 476 ms 42348 KB Output is correct
7 Correct 354 ms 38500 KB Output is correct
8 Correct 365 ms 38344 KB Output is correct
9 Correct 235 ms 37936 KB Output is correct
10 Correct 614 ms 35940 KB Output is correct
11 Correct 387 ms 37136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 448 ms 38988 KB Output is correct
3 Correct 507 ms 39540 KB Output is correct
4 Correct 585 ms 41896 KB Output is correct
5 Correct 779 ms 45216 KB Output is correct
6 Correct 442 ms 41588 KB Output is correct
7 Correct 478 ms 39204 KB Output is correct
8 Correct 414 ms 39200 KB Output is correct
9 Correct 253 ms 21156 KB Output is correct
10 Correct 718 ms 44732 KB Output is correct
11 Correct 360 ms 37168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 684 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 724 KB Output is correct
13 Correct 5 ms 596 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 324 ms 38208 KB Output is correct
18 Correct 616 ms 38144 KB Output is correct
19 Correct 347 ms 40484 KB Output is correct
20 Correct 377 ms 41940 KB Output is correct
21 Correct 391 ms 42028 KB Output is correct
22 Correct 855 ms 39344 KB Output is correct
23 Correct 379 ms 45460 KB Output is correct
24 Correct 303 ms 35432 KB Output is correct
25 Correct 728 ms 40220 KB Output is correct
26 Correct 686 ms 37168 KB Output is correct
27 Correct 781 ms 38140 KB Output is correct
28 Correct 556 ms 37752 KB Output is correct
29 Correct 331 ms 38524 KB Output is correct
30 Correct 230 ms 38072 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 441 ms 42256 KB Output is correct
33 Correct 485 ms 41752 KB Output is correct
34 Correct 682 ms 39664 KB Output is correct
35 Correct 454 ms 37228 KB Output is correct
36 Correct 476 ms 42348 KB Output is correct
37 Correct 354 ms 38500 KB Output is correct
38 Correct 365 ms 38344 KB Output is correct
39 Correct 235 ms 37936 KB Output is correct
40 Correct 614 ms 35940 KB Output is correct
41 Correct 387 ms 37136 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 448 ms 38988 KB Output is correct
44 Correct 507 ms 39540 KB Output is correct
45 Correct 585 ms 41896 KB Output is correct
46 Correct 779 ms 45216 KB Output is correct
47 Correct 442 ms 41588 KB Output is correct
48 Correct 478 ms 39204 KB Output is correct
49 Correct 414 ms 39200 KB Output is correct
50 Correct 253 ms 21156 KB Output is correct
51 Correct 718 ms 44732 KB Output is correct
52 Correct 360 ms 37168 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 2 ms 596 KB Output is correct
57 Correct 3 ms 596 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
59 Correct 3 ms 596 KB Output is correct
60 Correct 2 ms 596 KB Output is correct
61 Correct 3 ms 596 KB Output is correct
62 Correct 3 ms 724 KB Output is correct
63 Correct 3 ms 596 KB Output is correct
64 Correct 3 ms 724 KB Output is correct
65 Correct 5 ms 660 KB Output is correct
66 Correct 3 ms 596 KB Output is correct
67 Correct 324 ms 38212 KB Output is correct
68 Correct 548 ms 38116 KB Output is correct
69 Correct 350 ms 40520 KB Output is correct
70 Correct 385 ms 41816 KB Output is correct
71 Correct 360 ms 42032 KB Output is correct
72 Correct 730 ms 39352 KB Output is correct
73 Correct 355 ms 45344 KB Output is correct
74 Correct 272 ms 35416 KB Output is correct
75 Correct 679 ms 40252 KB Output is correct
76 Correct 614 ms 37216 KB Output is correct
77 Correct 618 ms 38140 KB Output is correct
78 Correct 534 ms 37768 KB Output is correct
79 Correct 304 ms 38404 KB Output is correct
80 Correct 242 ms 37960 KB Output is correct
81 Correct 440 ms 42168 KB Output is correct
82 Correct 463 ms 41720 KB Output is correct
83 Correct 666 ms 39672 KB Output is correct
84 Correct 412 ms 37180 KB Output is correct
85 Correct 477 ms 42376 KB Output is correct
86 Correct 381 ms 38392 KB Output is correct
87 Correct 354 ms 38416 KB Output is correct
88 Correct 586 ms 35916 KB Output is correct
89 Correct 388 ms 37076 KB Output is correct
90 Correct 458 ms 38912 KB Output is correct
91 Correct 456 ms 39624 KB Output is correct
92 Correct 502 ms 41912 KB Output is correct
93 Correct 821 ms 45112 KB Output is correct
94 Correct 436 ms 41548 KB Output is correct
95 Correct 481 ms 39320 KB Output is correct
96 Correct 293 ms 39168 KB Output is correct
97 Correct 239 ms 21124 KB Output is correct
98 Correct 699 ms 44732 KB Output is correct
99 Correct 373 ms 37120 KB Output is correct
100 Correct 43 ms 5300 KB Output is correct
101 Correct 463 ms 40300 KB Output is correct
102 Correct 634 ms 33888 KB Output is correct
103 Correct 436 ms 32508 KB Output is correct
104 Correct 484 ms 37792 KB Output is correct