Submission #1306034

#TimeUsernameProblemLanguageResultExecution timeMemory
1306034witek_cppStranded Far From Home (BOI22_island)C++20
100 / 100
241 ms42944 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) int(a.size())
#define f(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) f(i, 0, a)
#define pt(a) f(i, 0, a)
#define int ll
#define tv(a, x) for (auto& a : x)
#define DUZO 1000000000000000000LL
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

vi moc;
vi szef;
vvi wygryw;

int findd(int v) {
    if (szef[v] == v) return v;
    return (szef[v] = findd(szef[v]));
}


void unionn(int u, int v, int sila) { //u na pewno moze zdominowac v, sila - to jest ile musi miec v by zdominowac
    u = findd(u);
    v = findd(v);
    if (v == u) return; 
    if (moc[v] >= sila) { //bedzie wzajemna mozliwosc dominacji
        if (sz(wygryw[u]) < sz(wygryw[v])) {
            swap(u, v);
        }
        //cout << "lacze " << u << " z " << v << " nowym szefem bedzie " << u << "\n";
        tv(x, wygryw[v]) {
            wygryw[u].pb(x);
        }
    } 
    szef[v] = szef[u];
    moc[u] += moc[v];
    wygryw[v] = {};
}   

void solve() {
    int n, m;
    cin >> n >> m;
    vvi g(n + 1);
    vi a(n + 1);
    f(i, 1, n + 1) cin >> a[i];
    pt(m) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    vector<pii> p;
    f(i, 1, n+ 1) p.pb({a[i], i});
    sort(all(p));
    vi ak(n + 1, 0);
    szef = moc = {};
    wygryw = {{}};
    szef.resize(n + 1);
    wygryw.resize(n + 1);
    moc.resize(n + 1);
    pt(n + 1) wygryw[i] = {};
    tv(x, p) {
        int v = x.nd;
        int w = x.st;
        ak[v] = 1;
        moc[v] = w;
        szef[v] = v;
        wygryw[v].pb(v);
        tv(u, g[v]) {
            if (!ak[u]) continue;
            unionn(v, u, w);
        }
    }
    int x = findd(1);
    tv(z, wygryw[x]) {
        ak[z]= 2;
    }
    f(i, 1, n+1) {
        if (ak[i] == 2) {
            cout << "1";
        } else {
            cout << "0";
        }
    }
    return;
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q = 1;
    //cin >> q;
    while (q--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...