Submission #869483

# Submission time Handle Problem Language Result Execution time Memory
869483 2023-11-04T12:22:35 Z lolismek Stranded Far From Home (BOI22_island) C++14
0 / 100
1 ms 8540 KB
    #include <algorithm>
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <queue>

    #define int long long
    #define pii pair <int, int>


    using namespace std;

    //ifstream fin("input.in");
    //ofstream fout("output.out");

    #define fin cin
    #define fout cout

    const int NMAX = 2e5;

    int v[NMAX + 1];
    vector <int> adj[NMAX + 1];

    int par[NMAX + 1];
    int sum[NMAX + 1];

    int Find(int x){
        if(x == par[x]){
            return x;
        }
        return par[x] = Find(par[x]);
    }

    void Join(int x, int y){
        x = Find(x);
        y = Find(y);

        if(x == y){
            return;
        }

        par[x] = y;
        sum[y] += sum[x];
    }

    bool ins[NMAX + 1];

    void Insert(int x){
        ins[x] = true;
        for(int vec : adj[x]){
            if(ins[vec]){
                Join(vec, x);
            }
        }
    }

    bool ans[NMAX + 1];

    signed main(){

        int n, m;
        fin >> n >> m;

        vector <pii> aux;
        priority_queue <pii, vector <pii>, greater <pii>> pq;

        for(int i = 1; i <= n; i++){
            fin >> v[i];
            par[i] = i;
            sum[i] = v[i];
            aux.push_back({v[i], i});
            pq.push({v[i], i});
            ans[i] = 1;
        }

        for(int i = 1; i <= m; i++){
            int a, b;
            fin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        //cout << v[1] << ' ' << v[1010] << ' ' << v[1999] << ' ' << v[2000] << endl;

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

        for(int i = 0; i < (int)aux.size(); i++){
            //cout << i << endl;
            int val = aux[i].first;
            Insert(aux[i].second);
            //cout << "-- " << aux[i].second << endl;
            while(i + 1 < (int)aux.size() && aux[i + 1].first == aux[i].first){
                Insert(aux[++i].second);
                //cout << "-- " << aux[i].second << endl;
            }

            if(i == 1999){
                //cout << ") " << (int)pq.size() << endl;
            }

            vector <pii> toinsert;
            while(!pq.empty() && ((i == (int)aux.size() - 1 && pq.top().first >= val) || (i < (int)aux.size() - 1 && pq.top().first >= val && pq.top().first < aux[i + 1].first))){
                int node = pq.top().second;
                int x = pq.top().first;
                pq.pop();



                int s = sum[Find(node)];


                if(i == 1999){
                   // cout << ")) " << node << endl;
                }
                if(s == x){
                   // cout << ">> " << node << ' ' << val << ' ' << x << ' ' << s << endl;
                    ans[node] = 0;
                }else{
                    toinsert.push_back({s, node});
                }
            }

            for(pii x : toinsert){
                if(i == (int)aux.size() - 1){
                    continue;
                }
                if(x.first >= aux[i + 1].first){
                    pq.push(x);
                }else{
                    ans[x.second] = 0;
                }
            }
        }

        for(int i = 1; i <= n; i++){
            fout << ans[i];
        }
        fout << '\n';

        return 0;
    }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 1 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Incorrect 1 ms 8540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 1 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -