Submission #919693

# Submission time Handle Problem Language Result Execution time Memory
919693 2024-02-01T12:59:14 Z vjudge1 Stranded Far From Home (BOI22_island) C++17
100 / 100
244 ms 47244 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define ss(a) set<a>
#define pp(a, b) pair<a, b>
#define mm(a, b) map<a, b>
#define qq(a) queue<a>
#define pq(a) priority_queue<a>
#define ump(a, b) unordered_map<a, b>
#define ANDROID                   \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define elif else if
#define endl "\n"
#define pb push_back
#define ins insert
#define logi __lg
#define sqrt sqrtl
#define mpr make_pair
using namespace std;
using namespace __cxx11;
using namespace __gnu_pbds;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 5;
ll b[N];
struct Dsu
{
    ll n, ans;
    vv(ll) dsu, sum;
    vv(vv(ll)) s;
    Dsu(ll sz = 1)
    {
        n = sz;
        ans = n;
        dsu.resize(n + 1, 0);
        sum.resize(n + 1, 0);
        s.resize(n + 1);
        init();
    }
    void init()
    {
        for (ll i = 1; i <= n; i++)
            dsu[i] = -1, s[i] = {i};
    }
    bool Union(ll x, ll y)
    {
        x = repr(x), y = repr(y);
        if (x == y)
            return false;
        if (dsu[x] < dsu[y])
            swap(x, y);
        ans--;
        for (ll i : s[x])
            s[y].pb(i);
        s[x].clear();
        dsu[y] += dsu[x];
        sum[y] += sum[x];
        dsu[x] = y;
        return true;
    }
    ll repr(ll x)
    {
        if (dsu[x] < 0)
            return x;
        return dsu[x] = repr(dsu[x]);
    }
    ll query()
    {
        return ans;
    }
    ll size(ll x)
    {
        return -dsu[repr(x)];
    }
};
void solve()
{
    ll n, m;
    cin >> n >> m;
    pp(ll, ll) a[n + 1];
    vv(ll) g[n + 1];
    for (ll i = 1; i <= n; i++)
        cin >> a[i].F, a[i].S = i, b[i] = a[i].F;
    Dsu dsu(n);
    for (ll i = 1; i <= n; i++)
        dsu.sum[i] = b[i];
    while (m--)
    {
        ll a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    sort(a + 1, a + n + 1);
    ll ans[n + 1];
    for (ll i = 1; i <= n; i++)
        ans[i] = 1;
    for (ll _ = 1; _ <= n; _++)
    for (ll i : {a[_].S})
    for (ll j : g[i])
        if (b[j] <= b[i])
        {
            ll rprj = dsu.repr(j), rpri = dsu.repr(i);
            if (dsu.sum[rprj] < b[i])
            {
                for (ll k : dsu.s[rprj])
                    ans[k] = 0;
                dsu.s[rprj].clear();
            }
            dsu.Union(rprj, rpri);
        }
    for (ll i = 1; i <= n; i++)
        cout << ans[i];
    cout << endl;
}
/*

*/
int main()
{
    ANDROID
    // precomp();
    ll t = 1;
    // cin >> t;
    for (ll i = 1; i <= t; i++)
        // cout << "Case #" << i << ": ",
        solve();
    cerr << "\nTime elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 2 ms 856 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 2 ms 856 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 125 ms 39888 KB Output is correct
4 Correct 110 ms 37828 KB Output is correct
5 Correct 149 ms 41416 KB Output is correct
6 Correct 163 ms 42944 KB Output is correct
7 Correct 170 ms 42748 KB Output is correct
8 Correct 173 ms 39300 KB Output is correct
9 Correct 154 ms 47244 KB Output is correct
10 Correct 119 ms 37056 KB Output is correct
11 Correct 130 ms 39620 KB Output is correct
12 Correct 158 ms 38096 KB Output is correct
13 Correct 112 ms 36724 KB Output is correct
14 Correct 126 ms 37024 KB Output is correct
15 Correct 122 ms 38348 KB Output is correct
16 Correct 80 ms 37348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 190 ms 43412 KB Output is correct
3 Correct 236 ms 43716 KB Output is correct
4 Correct 138 ms 38340 KB Output is correct
5 Correct 134 ms 36848 KB Output is correct
6 Correct 186 ms 43716 KB Output is correct
7 Correct 131 ms 38188 KB Output is correct
8 Correct 131 ms 38340 KB Output is correct
9 Correct 79 ms 37296 KB Output is correct
10 Correct 126 ms 36552 KB Output is correct
11 Correct 136 ms 37340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 237 ms 40240 KB Output is correct
3 Correct 216 ms 40920 KB Output is correct
4 Correct 244 ms 43452 KB Output is correct
5 Correct 218 ms 45128 KB Output is correct
6 Correct 199 ms 40952 KB Output is correct
7 Correct 132 ms 40640 KB Output is correct
8 Correct 82 ms 37584 KB Output is correct
9 Correct 108 ms 22724 KB Output is correct
10 Correct 187 ms 45236 KB Output is correct
11 Correct 163 ms 37644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 2 ms 856 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 2 ms 856 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 125 ms 39888 KB Output is correct
18 Correct 110 ms 37828 KB Output is correct
19 Correct 149 ms 41416 KB Output is correct
20 Correct 163 ms 42944 KB Output is correct
21 Correct 170 ms 42748 KB Output is correct
22 Correct 173 ms 39300 KB Output is correct
23 Correct 154 ms 47244 KB Output is correct
24 Correct 119 ms 37056 KB Output is correct
25 Correct 130 ms 39620 KB Output is correct
26 Correct 158 ms 38096 KB Output is correct
27 Correct 112 ms 36724 KB Output is correct
28 Correct 126 ms 37024 KB Output is correct
29 Correct 122 ms 38348 KB Output is correct
30 Correct 80 ms 37348 KB Output is correct
31 Correct 1 ms 504 KB Output is correct
32 Correct 190 ms 43412 KB Output is correct
33 Correct 236 ms 43716 KB Output is correct
34 Correct 138 ms 38340 KB Output is correct
35 Correct 134 ms 36848 KB Output is correct
36 Correct 186 ms 43716 KB Output is correct
37 Correct 131 ms 38188 KB Output is correct
38 Correct 131 ms 38340 KB Output is correct
39 Correct 79 ms 37296 KB Output is correct
40 Correct 126 ms 36552 KB Output is correct
41 Correct 136 ms 37340 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 237 ms 40240 KB Output is correct
44 Correct 216 ms 40920 KB Output is correct
45 Correct 244 ms 43452 KB Output is correct
46 Correct 218 ms 45128 KB Output is correct
47 Correct 199 ms 40952 KB Output is correct
48 Correct 132 ms 40640 KB Output is correct
49 Correct 82 ms 37584 KB Output is correct
50 Correct 108 ms 22724 KB Output is correct
51 Correct 187 ms 45236 KB Output is correct
52 Correct 163 ms 37644 KB Output is correct
53 Correct 1 ms 344 KB Output is correct
54 Correct 1 ms 344 KB Output is correct
55 Correct 1 ms 600 KB Output is correct
56 Correct 1 ms 600 KB Output is correct
57 Correct 1 ms 856 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 1 ms 600 KB Output is correct
61 Correct 2 ms 600 KB Output is correct
62 Correct 2 ms 856 KB Output is correct
63 Correct 2 ms 856 KB Output is correct
64 Correct 1 ms 856 KB Output is correct
65 Correct 1 ms 600 KB Output is correct
66 Correct 1 ms 856 KB Output is correct
67 Correct 124 ms 39876 KB Output is correct
68 Correct 116 ms 37988 KB Output is correct
69 Correct 157 ms 41408 KB Output is correct
70 Correct 168 ms 42944 KB Output is correct
71 Correct 169 ms 42860 KB Output is correct
72 Correct 149 ms 39300 KB Output is correct
73 Correct 137 ms 47240 KB Output is correct
74 Correct 121 ms 37056 KB Output is correct
75 Correct 136 ms 39616 KB Output is correct
76 Correct 162 ms 38096 KB Output is correct
77 Correct 108 ms 36808 KB Output is correct
78 Correct 107 ms 36804 KB Output is correct
79 Correct 128 ms 38576 KB Output is correct
80 Correct 80 ms 37312 KB Output is correct
81 Correct 195 ms 43196 KB Output is correct
82 Correct 207 ms 43712 KB Output is correct
83 Correct 133 ms 38340 KB Output is correct
84 Correct 125 ms 36804 KB Output is correct
85 Correct 190 ms 43720 KB Output is correct
86 Correct 137 ms 38464 KB Output is correct
87 Correct 129 ms 38340 KB Output is correct
88 Correct 134 ms 36804 KB Output is correct
89 Correct 168 ms 37404 KB Output is correct
90 Correct 208 ms 40388 KB Output is correct
91 Correct 207 ms 41104 KB Output is correct
92 Correct 223 ms 43196 KB Output is correct
93 Correct 223 ms 45128 KB Output is correct
94 Correct 199 ms 40900 KB Output is correct
95 Correct 132 ms 40588 KB Output is correct
96 Correct 83 ms 37576 KB Output is correct
97 Correct 102 ms 22476 KB Output is correct
98 Correct 215 ms 45252 KB Output is correct
99 Correct 141 ms 37340 KB Output is correct
100 Correct 31 ms 7248 KB Output is correct
101 Correct 217 ms 41672 KB Output is correct
102 Correct 169 ms 34476 KB Output is correct
103 Correct 175 ms 33996 KB Output is correct
104 Correct 235 ms 39112 KB Output is correct