Submission #722146

# Submission time Handle Problem Language Result Execution time Memory
722146 2023-04-11T12:51:27 Z gagik_2007 Stranded Far From Home (BOI22_island) C++17
100 / 100
299 ms 45556 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll LG = 31;
ll n, m, k;
vector<ll>g[N];
ll a[N];
vector<ll>c[N];
ll p[N];
ll sz[N];
ll sum[N];
bool used[N];

void make_set(ll v) {
	p[v] = v;
	sz[v] = 1;
	sum[v] = a[v];
	c[v].push_back(v);
}

ll find_set(ll v) {
	if (p[v] == v)return v;
	return p[v] = find_set(p[v]);
}

void union_sets(ll v, ll u) {
	v = find_set(v);
	u = find_set(u);
	if (u != v) {
		if (sz[v] < sz[u]) {
			swap(v, u);
		}
		p[u] = v;
		sz[v] += sz[u];
		sum[v] += sum[u];
		sz[u] = 0;
		sum[u] = 0;
		if (c[v].size() < c[u].size()) {
			swap(c[v], c[u]);
		}
		for (ll x : c[u]) {
			c[v].push_back(x);
		}
		c[u].clear();
	}
}

priority_queue<pair<int, int>>deq;
bool used2[N];

string other_sol() {
	string ans = "";
	for (int u = 1; u <= n; u++) {
		for (int v = 1; v <= n; v++) {
			used2[v] = false;
		}
		while (!deq.empty()) {
			deq.pop();
		}
		used2[u] = true;
		ll cur = a[u];
		bool ok = true;
		for (int to : g[u]) {
			if (!used2[to]) {
				deq.push({ -a[to],to });
				used2[to] = true;
			}
		}
		while (!deq.empty()) {
			int v = deq.top().ss;
			deq.pop();
			if (a[v] > cur) {
				//cout << v << " " << a[v] << " " << cur << endl;
				ok = false;
				break;
			}
			cur += a[v];
			for (int to : g[v]) {
				if (!used2[to]) {
					deq.push({ -a[to],to });
					used2[to] = true;
				}
			}
		}
		for (int v = 1; v <= n; v++) {
			if (!used2[v]) {
				//cout << v << " " << "CHMTAV\n";
				ok = false;
				break;
			}
		}
		ans += ok + '0';
	}
	return ans;
}

string main_sol() {
	vector<ll>vs;
	ll summ = 0;
	for (ll i = 1; i <= n; i++) {
		make_set(i);
		summ += a[i];
		vs.push_back(i);
	}
	auto comp = [](ll x, ll y) {
		return a[x] < a[y];
	};
	sort(vs.begin(), vs.end(), comp);
	ll i = 0;
	ll cur = a[vs[i]];
	//cout << "OK" << endl;
	while (i < n) {
		ll j = i;
		while (i < n && a[vs[i]] == cur) {
			for (ll to : g[vs[i]]) {
				if (a[to] <= cur) {
					int papa = find_set(to);
					if (sum[papa] < a[vs[i]]) {
						for (int x : c[papa]) {
							used[x] = true;
						}
						c[papa].clear();
					}
					union_sets(vs[i], to);
				}
			}
			//cout << i << endl;
			i++;
		}
		cur = (i < n ? a[vs[i]] : summ);
	}
	string ans = "";
	if (sz[find_set(1)] != n) {
		for (ll i = 1; i <= n; i++) {
			ans += '0';
		}
	}
	else {
		for (ll i = 1; i <= n; i++) {
			ans += !used[i] + '0';
		}
	}
	return ans;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0); 
	cin >> n >> m;
	for (ll i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (ll i = 0; i < m; i++) {
		ll x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	string r1 = main_sol();
	cout << r1 << endl;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -

Compilation message

island.cpp: In function 'std::string main_sol()':
island.cpp:142:6: warning: unused variable 'j' [-Wunused-variable]
  142 |   ll j = i;
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 8 ms 9720 KB Output is correct
4 Correct 8 ms 10008 KB Output is correct
5 Correct 9 ms 9996 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 7 ms 9940 KB Output is correct
8 Correct 6 ms 9900 KB Output is correct
9 Correct 7 ms 9940 KB Output is correct
10 Correct 7 ms 10064 KB Output is correct
11 Correct 7 ms 9940 KB Output is correct
12 Correct 8 ms 10036 KB Output is correct
13 Correct 7 ms 10068 KB Output is correct
14 Correct 6 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 184 ms 38612 KB Output is correct
4 Correct 187 ms 42712 KB Output is correct
5 Correct 220 ms 41044 KB Output is correct
6 Correct 212 ms 42228 KB Output is correct
7 Correct 220 ms 42256 KB Output is correct
8 Correct 253 ms 41868 KB Output is correct
9 Correct 170 ms 45520 KB Output is correct
10 Correct 145 ms 35724 KB Output is correct
11 Correct 130 ms 38004 KB Output is correct
12 Correct 248 ms 38364 KB Output is correct
13 Correct 154 ms 43000 KB Output is correct
14 Correct 158 ms 42008 KB Output is correct
15 Correct 179 ms 37672 KB Output is correct
16 Correct 98 ms 36604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 236 ms 42828 KB Output is correct
3 Correct 282 ms 42948 KB Output is correct
4 Correct 170 ms 44412 KB Output is correct
5 Correct 162 ms 43484 KB Output is correct
6 Correct 244 ms 42696 KB Output is correct
7 Correct 162 ms 37600 KB Output is correct
8 Correct 197 ms 37652 KB Output is correct
9 Correct 91 ms 36660 KB Output is correct
10 Correct 159 ms 38376 KB Output is correct
11 Correct 188 ms 40764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 259 ms 39300 KB Output is correct
3 Correct 252 ms 40200 KB Output is correct
4 Correct 257 ms 41824 KB Output is correct
5 Correct 259 ms 44264 KB Output is correct
6 Correct 218 ms 40416 KB Output is correct
7 Correct 134 ms 39288 KB Output is correct
8 Correct 113 ms 41032 KB Output is correct
9 Correct 123 ms 26620 KB Output is correct
10 Correct 238 ms 44592 KB Output is correct
11 Correct 186 ms 40860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 8 ms 9720 KB Output is correct
4 Correct 8 ms 10008 KB Output is correct
5 Correct 9 ms 9996 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 7 ms 9940 KB Output is correct
8 Correct 6 ms 9900 KB Output is correct
9 Correct 7 ms 9940 KB Output is correct
10 Correct 7 ms 10064 KB Output is correct
11 Correct 7 ms 9940 KB Output is correct
12 Correct 8 ms 10036 KB Output is correct
13 Correct 7 ms 10068 KB Output is correct
14 Correct 6 ms 9940 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 184 ms 38612 KB Output is correct
18 Correct 187 ms 42712 KB Output is correct
19 Correct 220 ms 41044 KB Output is correct
20 Correct 212 ms 42228 KB Output is correct
21 Correct 220 ms 42256 KB Output is correct
22 Correct 253 ms 41868 KB Output is correct
23 Correct 170 ms 45520 KB Output is correct
24 Correct 145 ms 35724 KB Output is correct
25 Correct 130 ms 38004 KB Output is correct
26 Correct 248 ms 38364 KB Output is correct
27 Correct 154 ms 43000 KB Output is correct
28 Correct 158 ms 42008 KB Output is correct
29 Correct 179 ms 37672 KB Output is correct
30 Correct 98 ms 36604 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 236 ms 42828 KB Output is correct
33 Correct 282 ms 42948 KB Output is correct
34 Correct 170 ms 44412 KB Output is correct
35 Correct 162 ms 43484 KB Output is correct
36 Correct 244 ms 42696 KB Output is correct
37 Correct 162 ms 37600 KB Output is correct
38 Correct 197 ms 37652 KB Output is correct
39 Correct 91 ms 36660 KB Output is correct
40 Correct 159 ms 38376 KB Output is correct
41 Correct 188 ms 40764 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 259 ms 39300 KB Output is correct
44 Correct 252 ms 40200 KB Output is correct
45 Correct 257 ms 41824 KB Output is correct
46 Correct 259 ms 44264 KB Output is correct
47 Correct 218 ms 40416 KB Output is correct
48 Correct 134 ms 39288 KB Output is correct
49 Correct 113 ms 41032 KB Output is correct
50 Correct 123 ms 26620 KB Output is correct
51 Correct 238 ms 44592 KB Output is correct
52 Correct 186 ms 40860 KB Output is correct
53 Correct 8 ms 9684 KB Output is correct
54 Correct 5 ms 9684 KB Output is correct
55 Correct 5 ms 9684 KB Output is correct
56 Correct 6 ms 9908 KB Output is correct
57 Correct 7 ms 9940 KB Output is correct
58 Correct 7 ms 9940 KB Output is correct
59 Correct 6 ms 9940 KB Output is correct
60 Correct 6 ms 9868 KB Output is correct
61 Correct 6 ms 9940 KB Output is correct
62 Correct 7 ms 10036 KB Output is correct
63 Correct 7 ms 9940 KB Output is correct
64 Correct 7 ms 10068 KB Output is correct
65 Correct 6 ms 10032 KB Output is correct
66 Correct 7 ms 9940 KB Output is correct
67 Correct 148 ms 38648 KB Output is correct
68 Correct 170 ms 42732 KB Output is correct
69 Correct 207 ms 41048 KB Output is correct
70 Correct 198 ms 42220 KB Output is correct
71 Correct 243 ms 42292 KB Output is correct
72 Correct 225 ms 41900 KB Output is correct
73 Correct 168 ms 45556 KB Output is correct
74 Correct 192 ms 35772 KB Output is correct
75 Correct 134 ms 38072 KB Output is correct
76 Correct 235 ms 38392 KB Output is correct
77 Correct 176 ms 43084 KB Output is correct
78 Correct 157 ms 41944 KB Output is correct
79 Correct 155 ms 37564 KB Output is correct
80 Correct 100 ms 36652 KB Output is correct
81 Correct 234 ms 42776 KB Output is correct
82 Correct 230 ms 43020 KB Output is correct
83 Correct 192 ms 44452 KB Output is correct
84 Correct 148 ms 43468 KB Output is correct
85 Correct 254 ms 42740 KB Output is correct
86 Correct 189 ms 37576 KB Output is correct
87 Correct 169 ms 37564 KB Output is correct
88 Correct 179 ms 38336 KB Output is correct
89 Correct 193 ms 40728 KB Output is correct
90 Correct 256 ms 39204 KB Output is correct
91 Correct 251 ms 40196 KB Output is correct
92 Correct 263 ms 41764 KB Output is correct
93 Correct 236 ms 44304 KB Output is correct
94 Correct 239 ms 40324 KB Output is correct
95 Correct 148 ms 39124 KB Output is correct
96 Correct 110 ms 41084 KB Output is correct
97 Correct 125 ms 26632 KB Output is correct
98 Correct 232 ms 44524 KB Output is correct
99 Correct 181 ms 40828 KB Output is correct
100 Correct 51 ms 16244 KB Output is correct
101 Correct 299 ms 40580 KB Output is correct
102 Correct 183 ms 35408 KB Output is correct
103 Correct 215 ms 34444 KB Output is correct
104 Correct 258 ms 38364 KB Output is correct