Submission #674899

# Submission time Handle Problem Language Result Execution time Memory
674899 2022-12-26T13:32:04 Z Tigerpants Stranded Far From Home (BOI22_island) C++17
100 / 100
682 ms 100224 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <numeric>

using namespace std;

typedef long long int ll;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef vector<sll> vsll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef set<pll> spll;
typedef vector<spll> vspll;
typedef vector<bool> vb;

#define rep(x, a, b) for (int x = a; x < b; x++)
#define rep_itr(type_, x, b) for (type_::iterator x = b.begin(); x != b.end(); x++)
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define resz(a, b) a.resize(b)
#define sort_all(a) sort(all(a))

const ll MAXN = 200000;
const ll MAXM = 200000;

ll N, M;
vll s;
vsll g;

vb active;
vll ptr_comp;
vll sz_comp;
vsll takeover;
vll sum_comp;

ll find_par(ll x) {
    ll y = x;
    while (x != ptr_comp[x]) {
        x = ptr_comp[x];
    }
    ptr_comp[y] = x;
    return x;
}

void merge(ll x, ll y) { // assume both can takeover one another
    x = find_par(x);
    y = find_par(y);

    if (x == y) {return;}

    if (sz_comp[x] < sz_comp[y]) {swap(x, y);} // now x is larger component, y subordinate
    sz_comp[x] += sz_comp[y];
    ptr_comp[y] = x;
    takeover[x].insert(all(takeover[y]));
    sum_comp[x] += sum_comp[y];
}

void add_town(ll town) {
    // find all neighboring active components
    vll comps;
    rep_itr(sll, itr, g[town]) {
        if (active[*itr]) {
            comps.push_back(find_par(*itr));
        }
    }

    takeover[town].insert(town);
    active[town] = true;
    sum_comp[town] = s[town];

    // find all neighbors which cannot beat town, and make them subordinate
    rep_itr(vll, itr, comps) {
        if (sum_comp[*itr] < s[town]) {
            takeover[*itr].clear();
            merge(*itr, town);
        }
    }

    // find all other neighbors which can beat town, and merge
    rep_itr(vll, itr, comps) {
        merge(*itr, town);
    }
}

int main() {
    cin >> N >> M;
    resz(s, N);
    resz(g, N);

    rep(i, 0, N) {
        cin >> s[i];
    }

    ll tempa, tempb;
    rep(i, 0, M) {
        cin >> tempa >> tempb;
        tempa--; tempb--;
        g[tempa].insert(tempb);
        g[tempb].insert(tempa);
    }

    vpll sort_s(N);
    rep(i, 0, N) {
        sort_s[i] = mp(s[i], i);
    }

    sort_all(sort_s);

    resz(ptr_comp, N);
    resz(sz_comp, N);
    resz(takeover, N);
    resz(active, N);
    resz(sum_comp, N);
    fill_n(sz_comp.begin(), N, 1);
    rep(i, 0, N) {ptr_comp[i] = i;}
    fill_n(active.begin(), N, false);
    fill_n(sum_comp.begin(), N, 0);

    rep_itr(vpll, itr, sort_s) {
        add_town(itr->second);
    }

    // consider the component of the entire island, and which tie colors can take over
    ll island = find_par(0);
    rep(i, 0, N) {
        if (takeover[island].find(i) != takeover[island].end()) {
            cout << "1";
        } else {
            cout << "0";
        }
    }

    cout << endl;

    return 0;
}

// go in order of smallest to largest town, and activate it
// if it is connected to some activated component, it can take over that component
// if it is strictly larger than the entire component, the towns that can take over that component can't take over the current town

// datastructures:
// we need component --> smaller to larger merging
// each component needs size, and set of towns capable of taking it over
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 296 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 4 ms 956 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 3 ms 820 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 824 KB Output is correct
10 Correct 4 ms 1088 KB Output is correct
11 Correct 4 ms 980 KB Output is correct
12 Correct 3 ms 952 KB Output is correct
13 Correct 2 ms 952 KB Output is correct
14 Correct 3 ms 952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 381 ms 69772 KB Output is correct
4 Correct 351 ms 69304 KB Output is correct
5 Correct 503 ms 77984 KB Output is correct
6 Correct 533 ms 81864 KB Output is correct
7 Correct 567 ms 81220 KB Output is correct
8 Correct 398 ms 70624 KB Output is correct
9 Correct 454 ms 89012 KB Output is correct
10 Correct 414 ms 62372 KB Output is correct
11 Correct 475 ms 69600 KB Output is correct
12 Correct 444 ms 66472 KB Output is correct
13 Correct 294 ms 69124 KB Output is correct
14 Correct 343 ms 69348 KB Output is correct
15 Correct 376 ms 70640 KB Output is correct
16 Correct 299 ms 69188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 554 ms 89164 KB Output is correct
3 Correct 543 ms 89352 KB Output is correct
4 Correct 365 ms 70720 KB Output is correct
5 Correct 304 ms 69160 KB Output is correct
6 Correct 534 ms 88804 KB Output is correct
7 Correct 425 ms 70696 KB Output is correct
8 Correct 407 ms 70752 KB Output is correct
9 Correct 290 ms 69280 KB Output is correct
10 Correct 347 ms 65316 KB Output is correct
11 Correct 380 ms 69620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 572 ms 74628 KB Output is correct
3 Correct 595 ms 77692 KB Output is correct
4 Correct 667 ms 85536 KB Output is correct
5 Correct 680 ms 95940 KB Output is correct
6 Correct 582 ms 80624 KB Output is correct
7 Correct 489 ms 70548 KB Output is correct
8 Correct 249 ms 64844 KB Output is correct
9 Correct 397 ms 42744 KB Output is correct
10 Correct 601 ms 100200 KB Output is correct
11 Correct 363 ms 69364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 296 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 4 ms 956 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 3 ms 820 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 824 KB Output is correct
10 Correct 4 ms 1088 KB Output is correct
11 Correct 4 ms 980 KB Output is correct
12 Correct 3 ms 952 KB Output is correct
13 Correct 2 ms 952 KB Output is correct
14 Correct 3 ms 952 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 381 ms 69772 KB Output is correct
18 Correct 351 ms 69304 KB Output is correct
19 Correct 503 ms 77984 KB Output is correct
20 Correct 533 ms 81864 KB Output is correct
21 Correct 567 ms 81220 KB Output is correct
22 Correct 398 ms 70624 KB Output is correct
23 Correct 454 ms 89012 KB Output is correct
24 Correct 414 ms 62372 KB Output is correct
25 Correct 475 ms 69600 KB Output is correct
26 Correct 444 ms 66472 KB Output is correct
27 Correct 294 ms 69124 KB Output is correct
28 Correct 343 ms 69348 KB Output is correct
29 Correct 376 ms 70640 KB Output is correct
30 Correct 299 ms 69188 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 554 ms 89164 KB Output is correct
33 Correct 543 ms 89352 KB Output is correct
34 Correct 365 ms 70720 KB Output is correct
35 Correct 304 ms 69160 KB Output is correct
36 Correct 534 ms 88804 KB Output is correct
37 Correct 425 ms 70696 KB Output is correct
38 Correct 407 ms 70752 KB Output is correct
39 Correct 290 ms 69280 KB Output is correct
40 Correct 347 ms 65316 KB Output is correct
41 Correct 380 ms 69620 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 572 ms 74628 KB Output is correct
44 Correct 595 ms 77692 KB Output is correct
45 Correct 667 ms 85536 KB Output is correct
46 Correct 680 ms 95940 KB Output is correct
47 Correct 582 ms 80624 KB Output is correct
48 Correct 489 ms 70548 KB Output is correct
49 Correct 249 ms 64844 KB Output is correct
50 Correct 397 ms 42744 KB Output is correct
51 Correct 601 ms 100200 KB Output is correct
52 Correct 363 ms 69364 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 300 KB Output is correct
56 Correct 5 ms 852 KB Output is correct
57 Correct 5 ms 980 KB Output is correct
58 Correct 4 ms 852 KB Output is correct
59 Correct 4 ms 852 KB Output is correct
60 Correct 3 ms 724 KB Output is correct
61 Correct 2 ms 832 KB Output is correct
62 Correct 4 ms 980 KB Output is correct
63 Correct 4 ms 980 KB Output is correct
64 Correct 3 ms 952 KB Output is correct
65 Correct 2 ms 852 KB Output is correct
66 Correct 5 ms 944 KB Output is correct
67 Correct 378 ms 69836 KB Output is correct
68 Correct 341 ms 69296 KB Output is correct
69 Correct 534 ms 77976 KB Output is correct
70 Correct 548 ms 81844 KB Output is correct
71 Correct 561 ms 81184 KB Output is correct
72 Correct 390 ms 70604 KB Output is correct
73 Correct 463 ms 89052 KB Output is correct
74 Correct 373 ms 62400 KB Output is correct
75 Correct 510 ms 69732 KB Output is correct
76 Correct 424 ms 66536 KB Output is correct
77 Correct 301 ms 69200 KB Output is correct
78 Correct 338 ms 69268 KB Output is correct
79 Correct 370 ms 70784 KB Output is correct
80 Correct 344 ms 69224 KB Output is correct
81 Correct 528 ms 89124 KB Output is correct
82 Correct 552 ms 89300 KB Output is correct
83 Correct 387 ms 70660 KB Output is correct
84 Correct 302 ms 69100 KB Output is correct
85 Correct 546 ms 88968 KB Output is correct
86 Correct 401 ms 70636 KB Output is correct
87 Correct 408 ms 70628 KB Output is correct
88 Correct 345 ms 65328 KB Output is correct
89 Correct 357 ms 69628 KB Output is correct
90 Correct 608 ms 74776 KB Output is correct
91 Correct 566 ms 77736 KB Output is correct
92 Correct 639 ms 85428 KB Output is correct
93 Correct 682 ms 96124 KB Output is correct
94 Correct 587 ms 80720 KB Output is correct
95 Correct 465 ms 70580 KB Output is correct
96 Correct 253 ms 64852 KB Output is correct
97 Correct 401 ms 42820 KB Output is correct
98 Correct 647 ms 100224 KB Output is correct
99 Correct 362 ms 69520 KB Output is correct
100 Correct 373 ms 21824 KB Output is correct
101 Correct 622 ms 79324 KB Output is correct
102 Correct 514 ms 65416 KB Output is correct
103 Correct 522 ms 61384 KB Output is correct
104 Correct 579 ms 71228 KB Output is correct