Submission #733423

# Submission time Handle Problem Language Result Execution time Memory
733423 2023-04-30T17:00:52 Z speedyArda Stranded Far From Home (BOI22_island) C++14
0 / 100
572 ms 35164 KB
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const int MAXN = 2e5+5;
ll in[MAXN], sum[MAXN];
int ans[MAXN], par[MAXN];
vector<int> unprocessed[MAXN];

int find(int v)
{
    if(v == par[v])
        return v;
    return par[v] = find(par[v]);
}

struct cmp 
{
    bool operator()(const int &a, const int &b)
    const {
        if(sum[a] != sum[b])
            return sum[a] < sum[b];
        return a < b;
    }
};

set< pair<int, int> > available;
int merge(int a, int b)
{
    a = find(a), b = find(b);
    if(a == b)
        return a;
    if(unprocessed[a].size() < unprocessed[b].size())
        swap(a, b);
    for(int e : unprocessed[b])
        unprocessed[a].push_back(e);
    available.erase({sum[a], a});
    sum[a] += sum[b];
    available.erase({sum[b], b});
    sum[b] = 0;
    par[b] = a;
    available.insert({sum[a], a});
    unprocessed[b].clear();
    return a;
}
vector < vector< int > > adj(MAXN);
int main() 
{

    int n, m;
    cin >> n >> m;
    vector < pair<int, int> > sorted;
    for(int i = 1; i <= n; i++) {
        cin >> in[i];
        sorted.push_back({in[i], i});
        ans[i] = 1;
        sum[i] = in[i];
        par[i] = i;
        unprocessed[i].push_back(i);
    }
    sort(sorted.begin(), sorted.end());
    for(int i = 1; i <= m; i++)
    {
        int f, s;
        cin >> f >> s;
        adj[f].push_back(s);
        adj[s].push_back(f);
    }

    int last = 0;
    for(int i = 0; i < n; i++) 
    {
        last = i;
        while(i < n && sorted[last].first == sorted[i].first)
            i++;
        i--;
        while(last <= i)
        {
            int idx = sorted[last].second;
            //cout << in[idx] << " " << idx << "\n";
            for(int e : adj[idx])
            {
                if(in[e] <= in[idx]) {
                    merge(e, idx);
                    //cout << e << " " << idx << "\n";
                }
            }
            int curr = find(idx);
            if(available.find({sum[curr], curr}) == available.end())
                available.insert({sum[curr], curr});
            //cout << find(idx) << "\n";
            last++;
        }



        if(i < n - 1)
        {
            ll pass = sorted[i + 1].first;
            vector<int> remove;
            for(pair<int, int> e : available)
            {
                //cout << i << " " << e << " " << sum[e] << "\n";
                if(sum[e.second] < pass)
                {
                    remove.push_back(e.second);
                    for(int j : unprocessed[e.second])
                        ans[j] = 0;
                    unprocessed[e.second].clear();
                } else
                    break;
            }

            for(int e : remove)
            {
                available.erase({sum[e], e});
            }
        }
    }



    for(int i = 1; i <= n; i++)
        cout << ans[i];
    cout << "\n";

}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Incorrect 8 ms 9812 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Incorrect 314 ms 30300 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 510 ms 35164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 572 ms 32620 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Incorrect 8 ms 9812 KB Output isn't correct
5 Halted 0 ms 0 KB -