Submission #580165

# Submission time Handle Problem Language Result Execution time Memory
580165 2022-06-20T16:28:16 Z Josia Stranded Far From Home (BOI22_island) C++14
10 / 100
1000 ms 72652 KB
#include <bits/stdc++.h>

using namespace std;

#define int int64_t


struct unionfindElem {
    int chef;
    int amount;
    unordered_set<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(unordered_set<int>& a, unordered_set<int>& b) {
    if (a.size() > b.size()) {
        swap(a, b);
    }
    for (int i: a) b.insert(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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 5 ms 772 KB Output is correct
5 Correct 7 ms 852 KB Output is correct
6 Correct 8 ms 852 KB Output is correct
7 Correct 6 ms 724 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 6 ms 852 KB Output is correct
10 Correct 7 ms 852 KB Output is correct
11 Correct 6 ms 852 KB Output is correct
12 Correct 4 ms 964 KB Output is correct
13 Correct 10 ms 852 KB Output is correct
14 Correct 6 ms 852 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 629 ms 59360 KB Output is correct
4 Execution timed out 1086 ms 46652 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1089 ms 72652 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1075 ms 61064 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 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 1 ms 212 KB Output is correct
4 Correct 5 ms 772 KB Output is correct
5 Correct 7 ms 852 KB Output is correct
6 Correct 8 ms 852 KB Output is correct
7 Correct 6 ms 724 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 6 ms 852 KB Output is correct
10 Correct 7 ms 852 KB Output is correct
11 Correct 6 ms 852 KB Output is correct
12 Correct 4 ms 964 KB Output is correct
13 Correct 10 ms 852 KB Output is correct
14 Correct 6 ms 852 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 629 ms 59360 KB Output is correct
18 Execution timed out 1086 ms 46652 KB Time limit exceeded
19 Halted 0 ms 0 KB -