Submission #668158

# Submission time Handle Problem Language Result Execution time Memory
668158 2022-12-03T06:25:35 Z Sohsoh84 Stranded Far From Home (BOI22_island) C++17
100 / 100
488 ms 183620 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;
const ll LOG = 20;
const ll INF = 2e18;

// use sz instead of n :/
ll P[MAXN], n, m, F[MAXN], sz, val[MAXN], 
    par[MAXN][LOG], W[MAXN][LOG], S[MAXN];
vector<int> C[MAXN];
vector<pair<int, pll>> edges;

inline bool unite(int u, int v, int w) {
	u = P[u], v = P[v];
	if (u == v) return false;
	if (C[u].size() < C[v].size()) swap(u, v);

	sz++;
	par[F[u]][0] = par[F[v]][0] = sz;
	W[F[u]][0] = W[F[v]][0] = w;
	S[sz] = S[F[u]] + S[F[v]];

	F[u] = sz;
	for (int e : C[v]) {
		P[e] = u;
		C[u].push_back(e);
	}

	C[v].clear();
	return true;
}

inline int try_up(int v, ll val) {
	for (int i = LOG - 1; i >= 0; i--)
		if (W[v][i] <= val)
			v = par[v][i];

	return v;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> val[i];
		C[i] = {i};
		P[i] = F[i] = i;
		S[i] = val[i];
		sz++;
	}

	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		edges.push_back({int(max(val[v], val[u])), {u, v}});
	}

	sort(all(edges));
	for (auto [w, e] : edges)
		unite(e.X, e.Y, w);

	fill(W[0], W[0] + LOG, INF);
	W[sz][0] = INF;
	for (int i = 1; i < LOG; i++) {
		for (int v = 1; v <= sz; v++) {
			int p = par[v][i - 1];
			par[v][i] = par[p][i - 1];
			W[v][i] = W[p][i - 1];
		}
	}

	for (int i = 1; i <= n; i++) {
		int v = i;
		while (W[v][0] <= S[v])
			v = try_up(v, S[v]);

		cout << (v == sz ? 1 : 0);
	}

	cout << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 14 ms 23812 KB Output is correct
4 Correct 13 ms 25056 KB Output is correct
5 Correct 12 ms 25368 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 13 ms 25100 KB Output is correct
8 Correct 13 ms 24916 KB Output is correct
9 Correct 13 ms 25300 KB Output is correct
10 Correct 14 ms 25392 KB Output is correct
11 Correct 15 ms 25360 KB Output is correct
12 Correct 14 ms 25360 KB Output is correct
13 Correct 14 ms 25368 KB Output is correct
14 Correct 16 ms 25360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 14 ms 23744 KB Output is correct
3 Correct 413 ms 174512 KB Output is correct
4 Correct 330 ms 172940 KB Output is correct
5 Correct 428 ms 173792 KB Output is correct
6 Correct 441 ms 178440 KB Output is correct
7 Correct 405 ms 178904 KB Output is correct
8 Correct 439 ms 174328 KB Output is correct
9 Correct 328 ms 183596 KB Output is correct
10 Correct 349 ms 173400 KB Output is correct
11 Correct 376 ms 173228 KB Output is correct
12 Correct 413 ms 173796 KB Output is correct
13 Correct 362 ms 172784 KB Output is correct
14 Correct 369 ms 172880 KB Output is correct
15 Correct 396 ms 174312 KB Output is correct
16 Correct 348 ms 172176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 420 ms 179752 KB Output is correct
3 Correct 434 ms 180592 KB Output is correct
4 Correct 378 ms 174304 KB Output is correct
5 Correct 345 ms 172804 KB Output is correct
6 Correct 414 ms 180560 KB Output is correct
7 Correct 378 ms 174304 KB Output is correct
8 Correct 378 ms 174404 KB Output is correct
9 Correct 338 ms 172036 KB Output is correct
10 Correct 331 ms 173668 KB Output is correct
11 Correct 340 ms 173612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 454 ms 175340 KB Output is correct
3 Correct 465 ms 175960 KB Output is correct
4 Correct 467 ms 177876 KB Output is correct
5 Correct 440 ms 176552 KB Output is correct
6 Correct 407 ms 168264 KB Output is correct
7 Correct 357 ms 174212 KB Output is correct
8 Correct 320 ms 173088 KB Output is correct
9 Correct 257 ms 102436 KB Output is correct
10 Correct 453 ms 176200 KB Output is correct
11 Correct 393 ms 173616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 14 ms 23812 KB Output is correct
4 Correct 13 ms 25056 KB Output is correct
5 Correct 12 ms 25368 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 13 ms 25100 KB Output is correct
8 Correct 13 ms 24916 KB Output is correct
9 Correct 13 ms 25300 KB Output is correct
10 Correct 14 ms 25392 KB Output is correct
11 Correct 15 ms 25360 KB Output is correct
12 Correct 14 ms 25360 KB Output is correct
13 Correct 14 ms 25368 KB Output is correct
14 Correct 16 ms 25360 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 14 ms 23744 KB Output is correct
17 Correct 413 ms 174512 KB Output is correct
18 Correct 330 ms 172940 KB Output is correct
19 Correct 428 ms 173792 KB Output is correct
20 Correct 441 ms 178440 KB Output is correct
21 Correct 405 ms 178904 KB Output is correct
22 Correct 439 ms 174328 KB Output is correct
23 Correct 328 ms 183596 KB Output is correct
24 Correct 349 ms 173400 KB Output is correct
25 Correct 376 ms 173228 KB Output is correct
26 Correct 413 ms 173796 KB Output is correct
27 Correct 362 ms 172784 KB Output is correct
28 Correct 369 ms 172880 KB Output is correct
29 Correct 396 ms 174312 KB Output is correct
30 Correct 348 ms 172176 KB Output is correct
31 Correct 14 ms 23764 KB Output is correct
32 Correct 420 ms 179752 KB Output is correct
33 Correct 434 ms 180592 KB Output is correct
34 Correct 378 ms 174304 KB Output is correct
35 Correct 345 ms 172804 KB Output is correct
36 Correct 414 ms 180560 KB Output is correct
37 Correct 378 ms 174304 KB Output is correct
38 Correct 378 ms 174404 KB Output is correct
39 Correct 338 ms 172036 KB Output is correct
40 Correct 331 ms 173668 KB Output is correct
41 Correct 340 ms 173612 KB Output is correct
42 Correct 13 ms 23892 KB Output is correct
43 Correct 454 ms 175340 KB Output is correct
44 Correct 465 ms 175960 KB Output is correct
45 Correct 467 ms 177876 KB Output is correct
46 Correct 440 ms 176552 KB Output is correct
47 Correct 407 ms 168264 KB Output is correct
48 Correct 357 ms 174212 KB Output is correct
49 Correct 320 ms 173088 KB Output is correct
50 Correct 257 ms 102436 KB Output is correct
51 Correct 453 ms 176200 KB Output is correct
52 Correct 393 ms 173616 KB Output is correct
53 Correct 13 ms 23764 KB Output is correct
54 Correct 15 ms 23764 KB Output is correct
55 Correct 12 ms 23764 KB Output is correct
56 Correct 14 ms 25160 KB Output is correct
57 Correct 14 ms 25300 KB Output is correct
58 Correct 14 ms 24916 KB Output is correct
59 Correct 14 ms 25044 KB Output is correct
60 Correct 14 ms 24836 KB Output is correct
61 Correct 16 ms 25280 KB Output is correct
62 Correct 14 ms 25360 KB Output is correct
63 Correct 14 ms 25336 KB Output is correct
64 Correct 16 ms 25336 KB Output is correct
65 Correct 14 ms 25300 KB Output is correct
66 Correct 15 ms 25268 KB Output is correct
67 Correct 407 ms 174328 KB Output is correct
68 Correct 341 ms 173116 KB Output is correct
69 Correct 395 ms 173760 KB Output is correct
70 Correct 417 ms 178420 KB Output is correct
71 Correct 444 ms 178876 KB Output is correct
72 Correct 485 ms 174308 KB Output is correct
73 Correct 335 ms 183620 KB Output is correct
74 Correct 331 ms 173464 KB Output is correct
75 Correct 346 ms 173244 KB Output is correct
76 Correct 415 ms 173744 KB Output is correct
77 Correct 348 ms 172756 KB Output is correct
78 Correct 329 ms 172912 KB Output is correct
79 Correct 361 ms 174288 KB Output is correct
80 Correct 351 ms 171984 KB Output is correct
81 Correct 440 ms 179768 KB Output is correct
82 Correct 467 ms 180592 KB Output is correct
83 Correct 350 ms 174304 KB Output is correct
84 Correct 334 ms 172772 KB Output is correct
85 Correct 434 ms 180596 KB Output is correct
86 Correct 377 ms 174300 KB Output is correct
87 Correct 407 ms 174316 KB Output is correct
88 Correct 332 ms 173664 KB Output is correct
89 Correct 381 ms 173580 KB Output is correct
90 Correct 450 ms 175240 KB Output is correct
91 Correct 450 ms 175924 KB Output is correct
92 Correct 440 ms 177760 KB Output is correct
93 Correct 441 ms 176656 KB Output is correct
94 Correct 462 ms 168236 KB Output is correct
95 Correct 371 ms 174344 KB Output is correct
96 Correct 338 ms 173084 KB Output is correct
97 Correct 251 ms 102520 KB Output is correct
98 Correct 392 ms 176204 KB Output is correct
99 Correct 391 ms 173584 KB Output is correct
100 Correct 70 ms 33328 KB Output is correct
101 Correct 459 ms 177536 KB Output is correct
102 Correct 383 ms 143196 KB Output is correct
103 Correct 437 ms 151036 KB Output is correct
104 Correct 488 ms 172588 KB Output is correct