Submission #997640

# Submission time Handle Problem Language Result Execution time Memory
997640 2024-06-12T15:42:44 Z underwaterkillerwhale Stranded Far From Home (BOI22_island) C++17
0 / 100
668 ms 116160 KB
#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const int N  = 2e5 + 7;
const int Mod = 998244353;
const int szBL = 916;
const int INF = 1e9;
const int BASE = 137;

int n, m;
vector<int> ke[N];

int a[N];
pair<int,int> b[N];

bool Ans[N];
int larger[N];

struct Disjoin_set {
    int lab[N], sz[N], tot[N];
    vector<int> vec[N];
    set<pair<int,int>> Sadj[N];///greater val

    void init (int n) {
        rep (i, 1, n) {
            lab[i] = i, sz[i] = 1, tot[i] = a[i];
            vec[i] = {i};
            iter (&id, ke[i]) {
                Sadj[i].insert(mp(a[id], id));
            }
        }
    }

    int Find (int u) {
        return u == lab[u] ? u : Find(lab[u]);
    }

    void Join (int u, int v) {
        u = Find(u);
        v = Find(v);
        if (u == v) return;
        if (sz[u] < sz[v]) swap(u, v);
        lab[v] = u;
        sz[u] += sz[v];
        iter (&id, vec[v]) {
            iter (&idto, ke[id]) {
                Sadj[u].insert(mp(a[idto], idto));
            }
            vec[u].push_back(id);
        }
        tot[u] += tot[v];
        tot[u] = min(tot[u], INF);
        vector<int> ().swap(vec[v]);
    }

    int Find_Larger (int u) {
        int idx = u;
        u = Find(u);
        auto it = Sadj[u].lower_bound(mp(a[idx] + 1, -INF));
        if (it == Sadj[u].end()) return 0;
        else return it->se;
    }
}DSU;

void solution () {
    cin >> n >> m;
    rep (i, 1, n) {
        cin >> a[i];
        b[i].fs = a[i];
        b[i].se = i;
    }
    rep (i, 1, m) {
        int u, v;
        cin >> u >> v;
        ke[u].push_back(v);
        ke[v].push_back(u);
    }
    DSU.init(n);
    sort (b + 1, b + 1 + n);
    rep (i, 1, n) {
        static vector<int> vec; vec.clear();
        while (b[i].fs == b[i + 1].fs) vec.push_back(b[i++].se);
        vec.push_back(b[i].se);
        iter (&idx, vec) {
            iter (&to, ke[idx]) {
                if (a[idx] >= a[to]) DSU.Join (idx, to);
            }
        }
        iter (&idx, vec) {
            larger[idx] = DSU.Find_Larger(idx);
            if (DSU.tot[DSU.Find(idx)] < a[larger[idx]]) larger[idx] = -1;
        }
    }
    Ans[0] = 1;
    reb (i, n, 1) {
        if (Ans[i] == -1) Ans[i] = 0;
        else Ans[i] = Ans[larger[i]];
    }
    rep (i ,1, n) cout << Ans[i];
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    file ("c");
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
5 10
1 2
3 1
4 2
5 3
1 4
2 3
3 5
4 5
3 4
1 4

*/

Compilation message

island.cpp: In function 'void solution()':
island.cpp:113:20: warning: comparison of constant '-1' with boolean expression is always false [-Wbool-compare]
  113 |         if (Ans[i] == -1) Ans[i] = 0;
      |             ~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Incorrect 3 ms 24668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 24668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24668 KB Output is correct
2 Incorrect 668 ms 116160 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Incorrect 488 ms 88696 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Incorrect 3 ms 24668 KB Output isn't correct
3 Halted 0 ms 0 KB -