Submission #1355599

#TimeUsernameProblemLanguageResultExecution timeMemory
1355599iamhereforfunStranded Far From Home (BOI22_island)C++20
100 / 100
239 ms75724 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 2e5 + 5;
const int M = 2e6 + 5;
const long long K = (1LL << 60) - 1;
const int LG = 20;
const long long INF = 1e18 + 5;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 9;

int n, m, parent[N], s[N];
long long sum[N];
bool ans[N], vis[N];
vector<int> adj[N];
vector<pair<int, int>> v;
set<int> st[N];

int getParent(int i)
{
    return parent[i] == i ? i : parent[i] = getParent(parent[i]);
}

void update(int a, int b)
{
    a = getParent(a);
    b = getParent(b);
    if (a != b)
    {
        if (st[a].size() < st[b].size())
        {
            swap(a, b);
        }
        for (int i : st[b])
        {
            st[a].insert(i);
        }
        sum[a] += sum[b];
        parent[b] = a;
    }
}

inline void solve()
{
    cin >> n >> m;
    for (int x = 1; x <= n; x++)
    {
        int i;
        cin >> i;
        s[x] = i;
        sum[x] = i;
        parent[x] = x;
        st[x].insert(x);
        v.push_back({i, x});
    }
    for (int x = 0; x < m; x++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    sort(v.begin(), v.end());
    for (auto &[w, c] : v)
    {
        vis[c] = 1;
        for (int i : adj[c])
        {
            if (vis[i])
            {
                i = getParent(i);
                if (sum[i] < s[c])
                {
                    st[i].clear();
                }
                update(i, c);
            }
        }
    }
    int i = getParent(1);
    for (int j : st[i])
    {
        ans[j] = 1;
    }
    for (int x = 1; x <= n; x++)
    {
        cout << ans[x];
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...