Submission #591156

# Submission time Handle Problem Language Result Execution time Memory
591156 2022-07-06T23:20:54 Z tutis Stranded Far From Home (BOI22_island) C++17
100 / 100
273 ms 30092 KB
/*input
10 9
1 2 4 3 1 6 8 1 9 5
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
	ios_base::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	int S[N];
	for (int i = 0; i < N; i++)
		cin >> S[i];
	int pa[N];
	int sz[N];
	ll sum[N];
	for (int i = 0; i < N; i++)
	{
		pa[i] = i;
		sz[i] = 1;
		sum[i] = S[i];
	}
	vector<int>adj[N];
	while (M--)
	{
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	int w[N];
	int p[N];
	int mx[N];
	for (int i = 0; i < N; i++)
	{
		p[i] = i;
		mx[i] = i;
	}
	vector<array<int, 2>>last[N];
	sort(p, p + N, [&](int a, int b) {return S[a] < S[b];});
	for (int i : p)
	{
		for (int j : adj[i])
		{
			if (S[j] <= S[i])
			{
				int a = i;
				int b = j;
				while (pa[a] != a)
					a = pa[a];
				while (pa[b] != b)
					b = pa[b];
				if (a == b)
					continue;
				if (sz[a] > sz[b])
					swap(a, b);
				sz[b] += sz[a];
				pa[a] = b;
				w[a] = S[i];
				sum[b] += sum[a];
				last[b].push_back({a, mx[b]});
				if (S[mx[b]] < S[mx[a]])
					mx[b] = mx[a];
			}
		}
	}
	function<int(int)>root = [&](int i)
	{
		while (i != pa[i])
			i = pa[i];
		return i;
	};
	vector<bool>ret(N, false);
	vector<int>ok;
	ok.push_back(mx[root(0)]);
	vector<bool> inq(N, false);
	inq[mx[root(0)]] = true;
	while (!ok.empty())
	{
		int v = root(ok.back());
		ok.pop_back();
		int val = S[mx[v]];
		queue<int>Q;
		Q.push(root(v));
		vector<int>maxiai;
		while (!Q.empty())
		{
			int b = Q.front();
			Q.pop();
			if (sz[b] == 1 && S[b] == val)
				ret[b] = 1;
			if (last[b].size() > 0)
			{
				int a = last[b].back()[0];
				if (w[a] == val)
				{
					mx[b] = last[b].back()[1];
					last[b].pop_back();
					pa[a] = a;
					sz[b] -= sz[a];
					sum[b] -= sum[a];
					if (S[mx[a]] == val)
						Q.push(a);
					if (S[mx[b]] == val)
						Q.push(b);
					if (S[mx[a]] != val && sum[a] >= val)
					{
						if (inq[mx[a]] == false)
						{
							inq[mx[a]] = true;
							ok.push_back(mx[a]);
						}
					}
					if (S[mx[b]] != val && sum[b] >= val)
					{
						if (inq[mx[b]] == false)
						{
							inq[mx[a]] = true;
							ok.push_back(mx[b]);
						}
					}
				}
			}
		}
	}
	for (int i : ret)
		cout << i;
	cout << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 452 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 147 ms 25064 KB Output is correct
4 Correct 135 ms 26620 KB Output is correct
5 Correct 227 ms 26176 KB Output is correct
6 Correct 218 ms 27260 KB Output is correct
7 Correct 198 ms 28108 KB Output is correct
8 Correct 237 ms 28192 KB Output is correct
9 Correct 158 ms 28540 KB Output is correct
10 Correct 109 ms 28168 KB Output is correct
11 Correct 119 ms 27936 KB Output is correct
12 Correct 181 ms 27400 KB Output is correct
13 Correct 143 ms 26052 KB Output is correct
14 Correct 130 ms 27404 KB Output is correct
15 Correct 141 ms 27920 KB Output is correct
16 Correct 101 ms 26708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 199 ms 25296 KB Output is correct
3 Correct 246 ms 27976 KB Output is correct
4 Correct 157 ms 26748 KB Output is correct
5 Correct 126 ms 26304 KB Output is correct
6 Correct 235 ms 27604 KB Output is correct
7 Correct 177 ms 26600 KB Output is correct
8 Correct 177 ms 26532 KB Output is correct
9 Correct 100 ms 25656 KB Output is correct
10 Correct 157 ms 27000 KB Output is correct
11 Correct 147 ms 28016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 204 ms 25412 KB Output is correct
3 Correct 243 ms 28492 KB Output is correct
4 Correct 264 ms 26916 KB Output is correct
5 Correct 243 ms 29728 KB Output is correct
6 Correct 189 ms 27008 KB Output is correct
7 Correct 112 ms 26812 KB Output is correct
8 Correct 97 ms 27756 KB Output is correct
9 Correct 91 ms 14848 KB Output is correct
10 Correct 166 ms 26668 KB Output is correct
11 Correct 182 ms 26816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 452 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 147 ms 25064 KB Output is correct
18 Correct 135 ms 26620 KB Output is correct
19 Correct 227 ms 26176 KB Output is correct
20 Correct 218 ms 27260 KB Output is correct
21 Correct 198 ms 28108 KB Output is correct
22 Correct 237 ms 28192 KB Output is correct
23 Correct 158 ms 28540 KB Output is correct
24 Correct 109 ms 28168 KB Output is correct
25 Correct 119 ms 27936 KB Output is correct
26 Correct 181 ms 27400 KB Output is correct
27 Correct 143 ms 26052 KB Output is correct
28 Correct 130 ms 27404 KB Output is correct
29 Correct 141 ms 27920 KB Output is correct
30 Correct 101 ms 26708 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 199 ms 25296 KB Output is correct
33 Correct 246 ms 27976 KB Output is correct
34 Correct 157 ms 26748 KB Output is correct
35 Correct 126 ms 26304 KB Output is correct
36 Correct 235 ms 27604 KB Output is correct
37 Correct 177 ms 26600 KB Output is correct
38 Correct 177 ms 26532 KB Output is correct
39 Correct 100 ms 25656 KB Output is correct
40 Correct 157 ms 27000 KB Output is correct
41 Correct 147 ms 28016 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 204 ms 25412 KB Output is correct
44 Correct 243 ms 28492 KB Output is correct
45 Correct 264 ms 26916 KB Output is correct
46 Correct 243 ms 29728 KB Output is correct
47 Correct 189 ms 27008 KB Output is correct
48 Correct 112 ms 26812 KB Output is correct
49 Correct 97 ms 27756 KB Output is correct
50 Correct 91 ms 14848 KB Output is correct
51 Correct 166 ms 26668 KB Output is correct
52 Correct 182 ms 26816 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 2 ms 456 KB Output is correct
57 Correct 2 ms 616 KB Output is correct
58 Correct 1 ms 468 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 596 KB Output is correct
62 Correct 2 ms 596 KB Output is correct
63 Correct 2 ms 468 KB Output is correct
64 Correct 2 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 160 ms 27796 KB Output is correct
68 Correct 149 ms 27088 KB Output is correct
69 Correct 251 ms 25680 KB Output is correct
70 Correct 215 ms 27424 KB Output is correct
71 Correct 205 ms 26968 KB Output is correct
72 Correct 199 ms 27848 KB Output is correct
73 Correct 157 ms 28492 KB Output is correct
74 Correct 113 ms 26392 KB Output is correct
75 Correct 149 ms 26576 KB Output is correct
76 Correct 170 ms 26776 KB Output is correct
77 Correct 139 ms 25992 KB Output is correct
78 Correct 124 ms 26148 KB Output is correct
79 Correct 148 ms 26800 KB Output is correct
80 Correct 103 ms 27800 KB Output is correct
81 Correct 196 ms 27708 KB Output is correct
82 Correct 221 ms 28296 KB Output is correct
83 Correct 143 ms 24884 KB Output is correct
84 Correct 118 ms 26812 KB Output is correct
85 Correct 226 ms 30092 KB Output is correct
86 Correct 180 ms 28824 KB Output is correct
87 Correct 195 ms 28648 KB Output is correct
88 Correct 135 ms 26568 KB Output is correct
89 Correct 148 ms 25692 KB Output is correct
90 Correct 202 ms 27756 KB Output is correct
91 Correct 220 ms 29796 KB Output is correct
92 Correct 237 ms 29924 KB Output is correct
93 Correct 217 ms 28876 KB Output is correct
94 Correct 242 ms 27256 KB Output is correct
95 Correct 114 ms 28604 KB Output is correct
96 Correct 93 ms 27416 KB Output is correct
97 Correct 92 ms 15308 KB Output is correct
98 Correct 158 ms 28260 KB Output is correct
99 Correct 152 ms 28292 KB Output is correct
100 Correct 39 ms 4688 KB Output is correct
101 Correct 273 ms 29788 KB Output is correct
102 Correct 157 ms 21872 KB Output is correct
103 Correct 219 ms 21436 KB Output is correct
104 Correct 230 ms 25088 KB Output is correct