Submission #652048

# Submission time Handle Problem Language Result Execution time Memory
652048 2022-10-21T08:08:35 Z Vladth11 Stranded Far From Home (BOI22_island) C++14
100 / 100
507 ms 36996 KB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"

using namespace std;

const int NMAX = 200001;

class DSU{
public:
    int parent[NMAX], cnt[NMAX];
    long long suma[NMAX];
    vector <int> comp[NMAX];
    int root(int x){
        if(x == parent[x])
            return x;
        return parent[x] = root(parent[x]);
    }
    void merge(int a, int b){
        a = root(a);
        b = root(b);
        if(a == b)
            return;
        if(cnt[a] < cnt[b])
            swap(a, b);
        cnt[a] += cnt[b];
        parent[b] = a;
        suma[a] += suma[b];
        for(auto x : comp[b])
            comp[a].push_back(x);
        comp[b].clear();
    }
}dsu;

vector <int> v[NMAX];


int rez[NMAX];
int p[NMAX];
int idx[NMAX];

bool cmp(int a, int b){
    return p[a] < p[b];
}

int main()
{
    int n, m, i;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        cin >> p[i];
        rez[i] = 1;
        dsu.parent[i] = i;
        dsu.cnt[i] = 1;
        dsu.suma[i] = p[i];
        dsu.comp[i].clear();
        dsu.comp[i].push_back(i);
        idx[i] = i;
    }
    for(i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    sort(idx + 1, idx + n + 1, cmp);
    for(i = 1; i <= n; i++){
        int x = idx[i];
        for(auto vecin : v[x]){
            if(p[vecin] < p[x]){
                int rV = dsu.root(vecin);
                if(dsu.suma[rV] < p[x]){
                    for(auto y : dsu.comp[rV]){
                        rez[y] = 0;
                    }
                    dsu.comp[rV].clear();
                }
            }
        }
        for(auto vecin : v[x]){
            if(p[vecin] <= p[x]){
                dsu.merge(x, vecin);
            }
        }
    }
    set <int> st;
    for(i = 1; i <= n; i++)
        st.insert(dsu.root(i));
    for(i = 1; i <= n; i++){
        cout << rez[i] * (st.size() == 1);
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9812 KB Output is correct
5 Correct 9 ms 9848 KB Output is correct
6 Correct 8 ms 9812 KB Output is correct
7 Correct 8 ms 9852 KB Output is correct
8 Correct 9 ms 9824 KB Output is correct
9 Correct 8 ms 9940 KB Output is correct
10 Correct 9 ms 9992 KB Output is correct
11 Correct 10 ms 9940 KB Output is correct
12 Correct 8 ms 9968 KB Output is correct
13 Correct 8 ms 9908 KB Output is correct
14 Correct 8 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9668 KB Output is correct
3 Correct 304 ms 28852 KB Output is correct
4 Correct 320 ms 34808 KB Output is correct
5 Correct 354 ms 34472 KB Output is correct
6 Correct 395 ms 35244 KB Output is correct
7 Correct 353 ms 35172 KB Output is correct
8 Correct 431 ms 34832 KB Output is correct
9 Correct 331 ms 36304 KB Output is correct
10 Correct 336 ms 32324 KB Output is correct
11 Correct 242 ms 33332 KB Output is correct
12 Correct 374 ms 32616 KB Output is correct
13 Correct 267 ms 35224 KB Output is correct
14 Correct 285 ms 34828 KB Output is correct
15 Correct 293 ms 33228 KB Output is correct
16 Correct 220 ms 32332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 402 ms 31504 KB Output is correct
3 Correct 386 ms 35984 KB Output is correct
4 Correct 327 ms 36996 KB Output is correct
5 Correct 283 ms 35288 KB Output is correct
6 Correct 418 ms 36044 KB Output is correct
7 Correct 327 ms 33288 KB Output is correct
8 Correct 330 ms 33460 KB Output is correct
9 Correct 273 ms 32304 KB Output is correct
10 Correct 296 ms 33720 KB Output is correct
11 Correct 308 ms 34152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 447 ms 29276 KB Output is correct
3 Correct 407 ms 34096 KB Output is correct
4 Correct 413 ms 35052 KB Output is correct
5 Correct 385 ms 36004 KB Output is correct
6 Correct 345 ms 33792 KB Output is correct
7 Correct 284 ms 34360 KB Output is correct
8 Correct 227 ms 33276 KB Output is correct
9 Correct 245 ms 22944 KB Output is correct
10 Correct 336 ms 35060 KB Output is correct
11 Correct 317 ms 34248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9812 KB Output is correct
5 Correct 9 ms 9848 KB Output is correct
6 Correct 8 ms 9812 KB Output is correct
7 Correct 8 ms 9852 KB Output is correct
8 Correct 9 ms 9824 KB Output is correct
9 Correct 8 ms 9940 KB Output is correct
10 Correct 9 ms 9992 KB Output is correct
11 Correct 10 ms 9940 KB Output is correct
12 Correct 8 ms 9968 KB Output is correct
13 Correct 8 ms 9908 KB Output is correct
14 Correct 8 ms 9940 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 6 ms 9668 KB Output is correct
17 Correct 304 ms 28852 KB Output is correct
18 Correct 320 ms 34808 KB Output is correct
19 Correct 354 ms 34472 KB Output is correct
20 Correct 395 ms 35244 KB Output is correct
21 Correct 353 ms 35172 KB Output is correct
22 Correct 431 ms 34832 KB Output is correct
23 Correct 331 ms 36304 KB Output is correct
24 Correct 336 ms 32324 KB Output is correct
25 Correct 242 ms 33332 KB Output is correct
26 Correct 374 ms 32616 KB Output is correct
27 Correct 267 ms 35224 KB Output is correct
28 Correct 285 ms 34828 KB Output is correct
29 Correct 293 ms 33228 KB Output is correct
30 Correct 220 ms 32332 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 402 ms 31504 KB Output is correct
33 Correct 386 ms 35984 KB Output is correct
34 Correct 327 ms 36996 KB Output is correct
35 Correct 283 ms 35288 KB Output is correct
36 Correct 418 ms 36044 KB Output is correct
37 Correct 327 ms 33288 KB Output is correct
38 Correct 330 ms 33460 KB Output is correct
39 Correct 273 ms 32304 KB Output is correct
40 Correct 296 ms 33720 KB Output is correct
41 Correct 308 ms 34152 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 447 ms 29276 KB Output is correct
44 Correct 407 ms 34096 KB Output is correct
45 Correct 413 ms 35052 KB Output is correct
46 Correct 385 ms 36004 KB Output is correct
47 Correct 345 ms 33792 KB Output is correct
48 Correct 284 ms 34360 KB Output is correct
49 Correct 227 ms 33276 KB Output is correct
50 Correct 245 ms 22944 KB Output is correct
51 Correct 336 ms 35060 KB Output is correct
52 Correct 317 ms 34248 KB Output is correct
53 Correct 6 ms 9684 KB Output is correct
54 Correct 5 ms 9712 KB Output is correct
55 Correct 5 ms 9708 KB Output is correct
56 Correct 8 ms 9852 KB Output is correct
57 Correct 8 ms 9968 KB Output is correct
58 Correct 7 ms 9812 KB Output is correct
59 Correct 8 ms 9812 KB Output is correct
60 Correct 8 ms 9852 KB Output is correct
61 Correct 7 ms 9940 KB Output is correct
62 Correct 8 ms 9980 KB Output is correct
63 Correct 8 ms 9924 KB Output is correct
64 Correct 8 ms 9848 KB Output is correct
65 Correct 9 ms 9852 KB Output is correct
66 Correct 8 ms 9940 KB Output is correct
67 Correct 322 ms 33348 KB Output is correct
68 Correct 304 ms 34764 KB Output is correct
69 Correct 363 ms 34344 KB Output is correct
70 Correct 396 ms 35136 KB Output is correct
71 Correct 388 ms 35140 KB Output is correct
72 Correct 411 ms 34836 KB Output is correct
73 Correct 392 ms 36288 KB Output is correct
74 Correct 311 ms 32192 KB Output is correct
75 Correct 281 ms 33320 KB Output is correct
76 Correct 419 ms 32680 KB Output is correct
77 Correct 333 ms 35200 KB Output is correct
78 Correct 324 ms 34844 KB Output is correct
79 Correct 327 ms 33200 KB Output is correct
80 Correct 271 ms 32372 KB Output is correct
81 Correct 398 ms 35896 KB Output is correct
82 Correct 412 ms 36052 KB Output is correct
83 Correct 374 ms 36744 KB Output is correct
84 Correct 306 ms 35220 KB Output is correct
85 Correct 413 ms 36040 KB Output is correct
86 Correct 397 ms 33296 KB Output is correct
87 Correct 335 ms 33300 KB Output is correct
88 Correct 367 ms 33644 KB Output is correct
89 Correct 344 ms 34072 KB Output is correct
90 Correct 474 ms 33724 KB Output is correct
91 Correct 426 ms 34184 KB Output is correct
92 Correct 445 ms 35120 KB Output is correct
93 Correct 401 ms 36016 KB Output is correct
94 Correct 396 ms 33688 KB Output is correct
95 Correct 353 ms 34360 KB Output is correct
96 Correct 336 ms 33200 KB Output is correct
97 Correct 248 ms 23012 KB Output is correct
98 Correct 403 ms 35148 KB Output is correct
99 Correct 312 ms 34328 KB Output is correct
100 Correct 118 ms 14220 KB Output is correct
101 Correct 507 ms 34564 KB Output is correct
102 Correct 323 ms 29668 KB Output is correct
103 Correct 405 ms 29740 KB Output is correct
104 Correct 462 ms 33120 KB Output is correct