Submission #1027991

# Submission time Handle Problem Language Result Execution time Memory
1027991 2024-07-19T12:15:13 Z anango Stranded Far From Home (BOI22_island) C++17
10 / 100
1000 ms 56308 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
int INF = 1LL<<30;
vector<int> people;

vector<int> ans;
class DSU {
private:
    int n;
    vector<int> parent;
    vector<int> rank;
    vector<int> rank2;
    vector<set<int>> sets;
public:
    DSU(int sz) {
        n = sz;
        parent=vector<int>(n); iota(parent.begin(), parent.end(), (int)0);
        rank=vector<int>(n,1);
        for (int i=0; i<n; i++) rank[i] = people[i];
        rank2=vector<int>(n,1);
        sets=vector<set<int>>(n);
        for (int i=0; i<n; i++) sets[i] = {i};
    }
    int find(int node) {
        if (node==parent[node]) return node;
        return parent[node] = find(parent[node]);
    }
    void link(int u, int v) {
        u=find(u); v=find(v);
        if (u==v) {
            return;
        }
        if (rank2[u]<rank2[v]) {
            swap(u,v); swap(sets[u],sets[v]);
        }
        rank2[u]+=rank2[v]; rank2[v] = 0;
        rank[u]+=rank[v]; rank[v] = 0;
        for (int j:sets[v]) sets[u].insert(j);
        sets[v].clear();
        parent[v] = u;
    }
    int getrank(int u) {
        return rank[find(u)];
    }
    void destroy(int u) {
        sets[u].clear();
    }
    vector<set<int>> getsets() {
        return sets;
    }
};


signed main() {
    
    int local = 0;
    if (local) {
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    }
    /*#ifdef ONLINE_JUDGE
    	ios_base::sync_with_stdio(false);
    	cin.tie(NULL);
    #endif*/ //fast IO
    int n,m; cin >> n >> m;
    //want to calculate for each node, 
    //the smallest w such that if we remove all nodes of weight > w, the connected component containing this node
    //has total weight at most w
    //use DSU
    //make w take on the weight of each node + 1, in increasing order
    //any set that has sum at most the current w is not allowable
    //actually it's easier to merge the sets of indices from small to large
    //so a set is marked as bad when it is merged into a larger set
    ans=vector<int>(n,0);
    people=vector<int>(n); for (int i=0; i<n; i++) cin >> people[i];
    vector<int> order(n); iota(order.begin(), order.end(), (int)0);
    sort(order.begin(), order.end(), [&](const int i1, const int i2) {
        return people[i1]<people[i2];
    });
    vector<vector<int>> adjlist(n);
    for (int i=0; i<m; i++) {
        int u,v; cin >> u >> v; u--; v--;
        adjlist[u].push_back(v); adjlist[v].push_back(u);
    }

    DSU dsu(n);

    vector<int> active(n,0);
    for (int oi=0; oi<n; oi++) {
        int k = order[oi];
        //cout << "adding node " << oi <<" " << k <<" " << people[k] << endl;
        for (int j:adjlist[k]) {
            if (active[j]) {
                int r1 = dsu.getrank(j);
               // cout << "adding edge " << k <<" " << j <<" " << r1 << endl;
                if (r1<people[k]) {
                    dsu.destroy(dsu.find(j));
                }
                dsu.link(k,j);
            }
            else {

            
                //cout << "inactive edge " << k <<" " << j << endl;
            }
        }
        active[k] = 1;
    }
    vector<set<int>> settings = dsu.getsets();
    for (auto i:settings) {
        for (auto j:i) {
            ans[j] = 1;
        }
    }
    for (int i=0; i<n; i++) {
        cout << ans[i];
    }
    cout << endl;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 26 ms 796 KB Output is correct
5 Correct 10 ms 860 KB Output is correct
6 Correct 37 ms 820 KB Output is correct
7 Correct 21 ms 792 KB Output is correct
8 Correct 21 ms 732 KB Output is correct
9 Correct 34 ms 872 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 3 ms 860 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 13 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 1020 ms 42224 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 470 ms 55892 KB Output is correct
3 Correct 457 ms 56308 KB Output is correct
4 Execution timed out 1049 ms 41808 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Execution timed out 1081 ms 41700 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 26 ms 796 KB Output is correct
5 Correct 10 ms 860 KB Output is correct
6 Correct 37 ms 820 KB Output is correct
7 Correct 21 ms 792 KB Output is correct
8 Correct 21 ms 732 KB Output is correct
9 Correct 34 ms 872 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 3 ms 860 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 13 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Execution timed out 1020 ms 42224 KB Time limit exceeded
18 Halted 0 ms 0 KB -