Submission #672074

# Submission time Handle Problem Language Result Execution time Memory
672074 2022-12-14T16:53:30 Z LittleCube Stranded Far From Home (BOI22_island) C++14
100 / 100
387 ms 35440 KB
#include <bits/stdc++.h>
#define ll long long 
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

ll N, M, s[200005], dsu[200005], rk[200005], sum[200005], the[200005], ans[200005];
vector<int> E[200005], val, idx;
vector<pair<int, pii>> edge;
vector<pii> vertex, undo;

int find(int k)
{
	return k == dsu[k] ? k : find(dsu[k]);
}

void merge(int x, int y)
{
	int rx = find(x), ry = find(y);
	if(rx == ry)
		return;
	if(rk[rx] <= rk[ry])
	{
		rk[ry] += rk[rx];
		sum[ry] += sum[rx];
		dsu[rx] = ry;
		undo.emplace_back(pii(rx, ry));
	}
	else merge(ry, rx);
}

void rollback(int sz)
{
	while(undo.size() > sz)
	{
		//cout << "undo\n";
		auto [x, y] = undo.back();
		undo.pop_back();
		rk[y] -= rk[x];
		dsu[x] = x;
		sum[y] -= sum[x];
		the[x] = the[y];
	}
}

signed main()
{
	cin >> N >> M;
		
	for(int i = 1; i <= N; i++)
	{	
		cin >> s[i];
		val.emplace_back(s[i]);
		vertex.emplace_back(pii(s[i], i));
	}
	
	for(int i = 1; i <= N; i++)
		dsu[i] = i, rk[i] = 1, sum[i] = s[i], the[i] = 1e18;

	sort(val.begin(), val.end(), greater<int>());
	sort(vertex.begin(), vertex.end());
	val.resize(unique(val.begin(), val.end()) - val.begin());

	for(int i = 1; i <= M; i++)
	{
		int u, v;
		cin >> u >> v;
		E[v].emplace_back(u);
		E[u].emplace_back(v);
		edge.emplace_back(make_pair(max(s[u], s[v]), pii(u, v)));
	}	
	
	sort(edge.begin(), edge.end());

	for(int i = 0; i < M; i++)
	{
		idx.emplace_back(undo.size());
		merge(edge[i].S.F, edge[i].S.S);
	}

	for(ll k : val)
	{
		vector<int> strong;
		while(!vertex.empty() && vertex.back().F == k)
		{
			if(k == val.front())
				ans[vertex.back().S] = 1;
			else
			{
				int r = find(vertex.back().S);
				if(the[r] <= sum[r])
					ans[vertex.back().S] = 1;
			}
			if(ans[vertex.back().S] == 1)
				strong.push_back(vertex.back().S);
			vertex.pop_back();
		}
		while(!edge.empty() && edge.back().F == k)
		{
			auto [w, e] = edge.back();
			auto [u, v] = e;
			rollback(idx.back());
			edge.pop_back();
			idx.pop_back();
		}
		for(int i : strong)
		{
			for(int j : E[i])
			{
				int r = find(j);
				the[r] = min(the[r], k);
			}
		}
		//for(int i = 1; i <= N; i++)
		//	cout << "(" << dsu[i] << ", " << rk[i] << ", " << sum[i] << ", " << the[i] << ")" << " \n"[i == N];
	}

	for(int i = 1; i <= N; i++)
		cout << ans[i];
	cout << '\n';
}

Compilation message

island.cpp: In function 'void rollback(int)':
island.cpp:36:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |  while(undo.size() > sz)
      |        ~~~~~~~~~~~~^~~~
island.cpp:39:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   39 |   auto [x, y] = undo.back();
      |        ^
island.cpp: In function 'int main()':
island.cpp:102:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  102 |    auto [w, e] = edge.back();
      |         ^
island.cpp:103:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  103 |    auto [u, v] = e;
      |         ^
island.cpp:103:9: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
  103 |    auto [u, v] = e;
      |         ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5256 KB Output is correct
5 Correct 6 ms 5204 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 5 ms 5276 KB Output is correct
8 Correct 5 ms 5204 KB Output is correct
9 Correct 5 ms 5204 KB Output is correct
10 Correct 5 ms 5332 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 5 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4972 KB Output is correct
3 Correct 318 ms 34568 KB Output is correct
4 Correct 254 ms 31992 KB Output is correct
5 Correct 311 ms 33596 KB Output is correct
6 Correct 321 ms 34436 KB Output is correct
7 Correct 323 ms 34508 KB Output is correct
8 Correct 343 ms 34668 KB Output is correct
9 Correct 305 ms 33708 KB Output is correct
10 Correct 250 ms 33088 KB Output is correct
11 Correct 272 ms 34472 KB Output is correct
12 Correct 264 ms 31880 KB Output is correct
13 Correct 245 ms 31552 KB Output is correct
14 Correct 250 ms 31928 KB Output is correct
15 Correct 308 ms 34860 KB Output is correct
16 Correct 214 ms 33868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 358 ms 34432 KB Output is correct
3 Correct 356 ms 34804 KB Output is correct
4 Correct 318 ms 34756 KB Output is correct
5 Correct 240 ms 31576 KB Output is correct
6 Correct 352 ms 34724 KB Output is correct
7 Correct 387 ms 34756 KB Output is correct
8 Correct 328 ms 34720 KB Output is correct
9 Correct 218 ms 33776 KB Output is correct
10 Correct 276 ms 33964 KB Output is correct
11 Correct 256 ms 31804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 356 ms 34548 KB Output is correct
3 Correct 348 ms 34608 KB Output is correct
4 Correct 352 ms 34752 KB Output is correct
5 Correct 342 ms 34856 KB Output is correct
6 Correct 315 ms 33640 KB Output is correct
7 Correct 282 ms 35440 KB Output is correct
8 Correct 217 ms 32812 KB Output is correct
9 Correct 215 ms 23000 KB Output is correct
10 Correct 270 ms 31668 KB Output is correct
11 Correct 287 ms 31776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5256 KB Output is correct
5 Correct 6 ms 5204 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 5 ms 5276 KB Output is correct
8 Correct 5 ms 5204 KB Output is correct
9 Correct 5 ms 5204 KB Output is correct
10 Correct 5 ms 5332 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 5 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4972 KB Output is correct
17 Correct 318 ms 34568 KB Output is correct
18 Correct 254 ms 31992 KB Output is correct
19 Correct 311 ms 33596 KB Output is correct
20 Correct 321 ms 34436 KB Output is correct
21 Correct 323 ms 34508 KB Output is correct
22 Correct 343 ms 34668 KB Output is correct
23 Correct 305 ms 33708 KB Output is correct
24 Correct 250 ms 33088 KB Output is correct
25 Correct 272 ms 34472 KB Output is correct
26 Correct 264 ms 31880 KB Output is correct
27 Correct 245 ms 31552 KB Output is correct
28 Correct 250 ms 31928 KB Output is correct
29 Correct 308 ms 34860 KB Output is correct
30 Correct 214 ms 33868 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 358 ms 34432 KB Output is correct
33 Correct 356 ms 34804 KB Output is correct
34 Correct 318 ms 34756 KB Output is correct
35 Correct 240 ms 31576 KB Output is correct
36 Correct 352 ms 34724 KB Output is correct
37 Correct 387 ms 34756 KB Output is correct
38 Correct 328 ms 34720 KB Output is correct
39 Correct 218 ms 33776 KB Output is correct
40 Correct 276 ms 33964 KB Output is correct
41 Correct 256 ms 31804 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 356 ms 34548 KB Output is correct
44 Correct 348 ms 34608 KB Output is correct
45 Correct 352 ms 34752 KB Output is correct
46 Correct 342 ms 34856 KB Output is correct
47 Correct 315 ms 33640 KB Output is correct
48 Correct 282 ms 35440 KB Output is correct
49 Correct 217 ms 32812 KB Output is correct
50 Correct 215 ms 23000 KB Output is correct
51 Correct 270 ms 31668 KB Output is correct
52 Correct 287 ms 31776 KB Output is correct
53 Correct 3 ms 5008 KB Output is correct
54 Correct 3 ms 4948 KB Output is correct
55 Correct 3 ms 4948 KB Output is correct
56 Correct 5 ms 5220 KB Output is correct
57 Correct 5 ms 5272 KB Output is correct
58 Correct 5 ms 5256 KB Output is correct
59 Correct 5 ms 5204 KB Output is correct
60 Correct 5 ms 5148 KB Output is correct
61 Correct 5 ms 5204 KB Output is correct
62 Correct 5 ms 5204 KB Output is correct
63 Correct 6 ms 5204 KB Output is correct
64 Correct 5 ms 5204 KB Output is correct
65 Correct 5 ms 5204 KB Output is correct
66 Correct 5 ms 5204 KB Output is correct
67 Correct 299 ms 34692 KB Output is correct
68 Correct 254 ms 31892 KB Output is correct
69 Correct 306 ms 33624 KB Output is correct
70 Correct 361 ms 34516 KB Output is correct
71 Correct 324 ms 34432 KB Output is correct
72 Correct 326 ms 34760 KB Output is correct
73 Correct 311 ms 33736 KB Output is correct
74 Correct 253 ms 33072 KB Output is correct
75 Correct 269 ms 34480 KB Output is correct
76 Correct 268 ms 31924 KB Output is correct
77 Correct 237 ms 31564 KB Output is correct
78 Correct 244 ms 31884 KB Output is correct
79 Correct 303 ms 34668 KB Output is correct
80 Correct 210 ms 33764 KB Output is correct
81 Correct 368 ms 34552 KB Output is correct
82 Correct 353 ms 34688 KB Output is correct
83 Correct 324 ms 34804 KB Output is correct
84 Correct 244 ms 31660 KB Output is correct
85 Correct 356 ms 34868 KB Output is correct
86 Correct 362 ms 34676 KB Output is correct
87 Correct 335 ms 34860 KB Output is correct
88 Correct 274 ms 33988 KB Output is correct
89 Correct 261 ms 31896 KB Output is correct
90 Correct 354 ms 34632 KB Output is correct
91 Correct 334 ms 34660 KB Output is correct
92 Correct 370 ms 34824 KB Output is correct
93 Correct 338 ms 34680 KB Output is correct
94 Correct 312 ms 33640 KB Output is correct
95 Correct 277 ms 35372 KB Output is correct
96 Correct 207 ms 32720 KB Output is correct
97 Correct 201 ms 22964 KB Output is correct
98 Correct 263 ms 31636 KB Output is correct
99 Correct 260 ms 31988 KB Output is correct
100 Correct 129 ms 13524 KB Output is correct
101 Correct 372 ms 34748 KB Output is correct
102 Correct 294 ms 30204 KB Output is correct
103 Correct 336 ms 31172 KB Output is correct
104 Correct 361 ms 34376 KB Output is correct