답안 #733410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
733410 2023-04-30T16:36:56 Z speedyArda Stranded Far From Home (BOI22_island) C++14
0 / 100
555 ms 37876 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<int, cmp> 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);
    sum[a] += sum[b];
    sum[b] = 0;
    par[b] = a;
    available.erase(b);
    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";
                }
            }
            //cout << find(idx) << "\n";
            available.erase(find(idx));
            available.insert(find(idx));
            last++;
        }



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

            for(int e : remove)
            {
                available.erase(e);
            }
        }
    }



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

}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Incorrect 8 ms 9880 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 331 ms 30384 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 555 ms 37876 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9696 KB Output is correct
2 Incorrect 549 ms 34868 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Incorrect 8 ms 9880 KB Output isn't correct
5 Halted 0 ms 0 KB -