Submission #578269

# Submission time Handle Problem Language Result Execution time Memory
578269 2022-06-16T09:46:44 Z kingfran1907 Stranded Far From Home (BOI22_island) C++14
100 / 100
798 ms 72324 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;

int n, m;
vector< int > graph[maxn];
int niz[maxn];
bool bio[maxn];
int parr[maxn];
llint siz[maxn];
vector< int > s[maxn];
set< pair<int, int> > graph2[maxn];
int whs[maxn];
vector< pair<int, int> > v;
map< int, vector<int> > ms;

int fin(int x) {
	if (x == parr[x]) return x;
	return parr[x] = fin(parr[x]);
}

void merg(int a, int b, int val) {
	a = fin(a);
	b = fin(b);
	if (a == b) return;
	
	graph2[a].erase({niz[b], b});
	graph2[b].erase({niz[a], a});
	while (!graph2[a].empty() && graph2[a].begin()->first <= val) graph2[a].erase(graph2[a].begin());
	while (!graph2[b].empty() && graph2[b].begin()->first <= val) graph2[b].erase(graph2[b].begin());
	
	if (graph2[a].size() < graph2[b].size()) swap(a, b);
	parr[b] = a;
	siz[a] += siz[b];
	for (auto iter : graph2[b]) {
		graph2[a].insert(iter);
	}
	
	int pa = whs[a], pb = whs[b];
	if (s[pa].size() < s[pb].size()) swap(pa, pb);
	whs[a] = pa;
	for (int ac : s[pb]) s[pa].push_back(ac);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d", niz+i);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	
	for (int i = 1; i <= n; i++) {
		parr[i] = i, siz[i] = niz[i];
		v.push_back({siz[i], i});
		s[i].push_back(i); whs[i] = i;
		ms[niz[i]].push_back(i);
		
		for (int tren : graph[i]) 
			if (niz[tren] > niz[i]) graph2[i].insert({niz[tren], tren});
		//for (auto tren : graph2[i]) printf("%d ", tren.second); printf("\n");
	}
	sort(v.begin(), v.end());
	
	for (int i = 1; i <= n; i++) bio[i] = true;
	for (auto iter : ms) {
		vector< int > ts = iter.second;
		int val = iter.first;
		
		for (int node : ts) {
			for (int tren : graph[node]) {
				if (niz[tren] <= val) {
					merg(node, tren, val);
					//printf("merging: %d %d\n", node, tren);
				}
			}
		}
		
		for (int node : ts) {
			//printf("trying: %d\n", node);
			int ac = fin(node);	
			//if (!graph2[ac].empty()) printf("smallest neighbor: %d\n", graph2[ac].begin()->second);
			if (!graph2[ac].empty() && graph2[ac].begin()->first > siz[ac]) {
				//printf("not able (%d %d) %d\n", graph2[ac].begin()->first, graph2[ac].begin()->second, siz[ac]);
				int ind = whs[ac];
				for (int iter : s[ind]) {
					bio[iter] = false;
				}
				s[ind].clear();
			}
		}
	}
	for (int i = 1; i <= n; i++) printf("%d", bio[i]);
	return 0;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
island.cpp:50:36: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |  for (int i = 1; i <= n; i++) scanf("%d", niz+i);
      |                               ~~~~~^~~~~~~~~~~~~
island.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 13 ms 19028 KB Output is correct
3 Correct 12 ms 19028 KB Output is correct
4 Correct 12 ms 19560 KB Output is correct
5 Correct 12 ms 19540 KB Output is correct
6 Correct 12 ms 19284 KB Output is correct
7 Correct 13 ms 19420 KB Output is correct
8 Correct 15 ms 19520 KB Output is correct
9 Correct 12 ms 19412 KB Output is correct
10 Correct 13 ms 19584 KB Output is correct
11 Correct 12 ms 19540 KB Output is correct
12 Correct 12 ms 19540 KB Output is correct
13 Correct 11 ms 19296 KB Output is correct
14 Correct 14 ms 19332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19064 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 333 ms 70308 KB Output is correct
4 Correct 224 ms 41308 KB Output is correct
5 Correct 436 ms 70128 KB Output is correct
6 Correct 441 ms 66588 KB Output is correct
7 Correct 407 ms 71848 KB Output is correct
8 Correct 285 ms 41688 KB Output is correct
9 Correct 304 ms 67332 KB Output is correct
10 Correct 333 ms 64444 KB Output is correct
11 Correct 243 ms 42464 KB Output is correct
12 Correct 294 ms 41600 KB Output is correct
13 Correct 206 ms 41532 KB Output is correct
14 Correct 217 ms 41364 KB Output is correct
15 Correct 299 ms 70580 KB Output is correct
16 Correct 239 ms 70040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 652 ms 72224 KB Output is correct
3 Correct 608 ms 67384 KB Output is correct
4 Correct 213 ms 41532 KB Output is correct
5 Correct 209 ms 41324 KB Output is correct
6 Correct 678 ms 72320 KB Output is correct
7 Correct 326 ms 70540 KB Output is correct
8 Correct 328 ms 70548 KB Output is correct
9 Correct 240 ms 70036 KB Output is correct
10 Correct 303 ms 59872 KB Output is correct
11 Correct 278 ms 48532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 432 ms 48460 KB Output is correct
3 Correct 397 ms 48788 KB Output is correct
4 Correct 462 ms 49920 KB Output is correct
5 Correct 319 ms 44204 KB Output is correct
6 Correct 323 ms 41728 KB Output is correct
7 Correct 203 ms 42424 KB Output is correct
8 Correct 164 ms 48816 KB Output is correct
9 Correct 232 ms 40380 KB Output is correct
10 Correct 282 ms 45760 KB Output is correct
11 Correct 289 ms 47600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 13 ms 19028 KB Output is correct
3 Correct 12 ms 19028 KB Output is correct
4 Correct 12 ms 19560 KB Output is correct
5 Correct 12 ms 19540 KB Output is correct
6 Correct 12 ms 19284 KB Output is correct
7 Correct 13 ms 19420 KB Output is correct
8 Correct 15 ms 19520 KB Output is correct
9 Correct 12 ms 19412 KB Output is correct
10 Correct 13 ms 19584 KB Output is correct
11 Correct 12 ms 19540 KB Output is correct
12 Correct 12 ms 19540 KB Output is correct
13 Correct 11 ms 19296 KB Output is correct
14 Correct 14 ms 19332 KB Output is correct
15 Correct 13 ms 19064 KB Output is correct
16 Correct 10 ms 19028 KB Output is correct
17 Correct 333 ms 70308 KB Output is correct
18 Correct 224 ms 41308 KB Output is correct
19 Correct 436 ms 70128 KB Output is correct
20 Correct 441 ms 66588 KB Output is correct
21 Correct 407 ms 71848 KB Output is correct
22 Correct 285 ms 41688 KB Output is correct
23 Correct 304 ms 67332 KB Output is correct
24 Correct 333 ms 64444 KB Output is correct
25 Correct 243 ms 42464 KB Output is correct
26 Correct 294 ms 41600 KB Output is correct
27 Correct 206 ms 41532 KB Output is correct
28 Correct 217 ms 41364 KB Output is correct
29 Correct 299 ms 70580 KB Output is correct
30 Correct 239 ms 70040 KB Output is correct
31 Correct 11 ms 19028 KB Output is correct
32 Correct 652 ms 72224 KB Output is correct
33 Correct 608 ms 67384 KB Output is correct
34 Correct 213 ms 41532 KB Output is correct
35 Correct 209 ms 41324 KB Output is correct
36 Correct 678 ms 72320 KB Output is correct
37 Correct 326 ms 70540 KB Output is correct
38 Correct 328 ms 70548 KB Output is correct
39 Correct 240 ms 70036 KB Output is correct
40 Correct 303 ms 59872 KB Output is correct
41 Correct 278 ms 48532 KB Output is correct
42 Correct 13 ms 19028 KB Output is correct
43 Correct 432 ms 48460 KB Output is correct
44 Correct 397 ms 48788 KB Output is correct
45 Correct 462 ms 49920 KB Output is correct
46 Correct 319 ms 44204 KB Output is correct
47 Correct 323 ms 41728 KB Output is correct
48 Correct 203 ms 42424 KB Output is correct
49 Correct 164 ms 48816 KB Output is correct
50 Correct 232 ms 40380 KB Output is correct
51 Correct 282 ms 45760 KB Output is correct
52 Correct 289 ms 47600 KB Output is correct
53 Correct 11 ms 19108 KB Output is correct
54 Correct 12 ms 19028 KB Output is correct
55 Correct 12 ms 19028 KB Output is correct
56 Correct 13 ms 19540 KB Output is correct
57 Correct 13 ms 19552 KB Output is correct
58 Correct 14 ms 19284 KB Output is correct
59 Correct 16 ms 19444 KB Output is correct
60 Correct 15 ms 19524 KB Output is correct
61 Correct 12 ms 19368 KB Output is correct
62 Correct 12 ms 19540 KB Output is correct
63 Correct 13 ms 19540 KB Output is correct
64 Correct 12 ms 19592 KB Output is correct
65 Correct 11 ms 19264 KB Output is correct
66 Correct 12 ms 19316 KB Output is correct
67 Correct 312 ms 70372 KB Output is correct
68 Correct 229 ms 41424 KB Output is correct
69 Correct 378 ms 70136 KB Output is correct
70 Correct 441 ms 66540 KB Output is correct
71 Correct 404 ms 71852 KB Output is correct
72 Correct 270 ms 41692 KB Output is correct
73 Correct 302 ms 67320 KB Output is correct
74 Correct 347 ms 64432 KB Output is correct
75 Correct 233 ms 42356 KB Output is correct
76 Correct 283 ms 41520 KB Output is correct
77 Correct 232 ms 41668 KB Output is correct
78 Correct 187 ms 41352 KB Output is correct
79 Correct 305 ms 70584 KB Output is correct
80 Correct 244 ms 70032 KB Output is correct
81 Correct 623 ms 72096 KB Output is correct
82 Correct 642 ms 67368 KB Output is correct
83 Correct 233 ms 41552 KB Output is correct
84 Correct 249 ms 41316 KB Output is correct
85 Correct 622 ms 72324 KB Output is correct
86 Correct 329 ms 70640 KB Output is correct
87 Correct 351 ms 70492 KB Output is correct
88 Correct 272 ms 59884 KB Output is correct
89 Correct 307 ms 48568 KB Output is correct
90 Correct 391 ms 48560 KB Output is correct
91 Correct 461 ms 48828 KB Output is correct
92 Correct 391 ms 49848 KB Output is correct
93 Correct 278 ms 44140 KB Output is correct
94 Correct 326 ms 41764 KB Output is correct
95 Correct 188 ms 42320 KB Output is correct
96 Correct 173 ms 48776 KB Output is correct
97 Correct 266 ms 40376 KB Output is correct
98 Correct 289 ms 45580 KB Output is correct
99 Correct 311 ms 47520 KB Output is correct
100 Correct 116 ms 31436 KB Output is correct
101 Correct 718 ms 65812 KB Output is correct
102 Correct 235 ms 37544 KB Output is correct
103 Correct 612 ms 61804 KB Output is correct
104 Correct 798 ms 69324 KB Output is correct