#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int *ej[N], eo[N], *ej_[N], eo_[N];
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int ds[N], rr[N];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i), j = find(j);
if (i == j || rr[j] == -1)
return;
append(ej_, eo_, rr[i], rr[j]);
if (ds[i] > ds[j])
ds[i] = j, rr[j] = rr[i];
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
}
long long sz[N];
void dfs1(int i) {
int o;
sz[i] = aa[i];
for (o = eo_[i]; o--; ) {
int j = ej_[i][o];
dfs1(j);
sz[i] += sz[j];
}
}
char cc[N + 1];
void dfs2(int i) {
int o;
cc[i] = 1;
for (o = eo_[i]; o--; ) {
int j = ej_[i][o];
if (sz[j] >= aa[i])
dfs2(j);
}
}
int main() {
static int ii[N];
int n, m, h, i, j, o;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
while (m--) {
scanf("%d%d", &i, &j), i--, j--;
append(ej, eo, i, j), append(ej, eo, j, i);
}
memset(ds, -1, n * sizeof *ds), memset(rr, -1, n * sizeof *rr);
for (i = 0; i < n; i++)
ej_[i] = (int *) malloc(2 * sizeof *ej_[i]);
for (h = 0; h < n; h++) {
i = ii[h];
rr[i] = i;
for (o = eo[i]; o--; ) {
j = ej[i][o];
join(i, j);
}
}
i = rr[find(0)];
dfs1(i), dfs2(i);
for (i = 0; i < n; i++)
cc[i] += '0';
printf("%s\n", cc);
return 0;
}
Compilation message
island.c: In function 'append':
island.c:39:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
39 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
island.c: In function 'main':
island.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
island.c:98:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
island.c:105:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
560 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
159 ms |
28644 KB |
Output is correct |
4 |
Correct |
117 ms |
35352 KB |
Output is correct |
5 |
Correct |
158 ms |
23440 KB |
Output is correct |
6 |
Correct |
147 ms |
24628 KB |
Output is correct |
7 |
Correct |
152 ms |
25292 KB |
Output is correct |
8 |
Correct |
146 ms |
37804 KB |
Output is correct |
9 |
Correct |
137 ms |
24460 KB |
Output is correct |
10 |
Correct |
160 ms |
26520 KB |
Output is correct |
11 |
Correct |
115 ms |
26268 KB |
Output is correct |
12 |
Correct |
151 ms |
25920 KB |
Output is correct |
13 |
Correct |
124 ms |
36360 KB |
Output is correct |
14 |
Correct |
110 ms |
36144 KB |
Output is correct |
15 |
Correct |
128 ms |
38580 KB |
Output is correct |
16 |
Correct |
98 ms |
37296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
212 ms |
22444 KB |
Output is correct |
3 |
Correct |
180 ms |
25080 KB |
Output is correct |
4 |
Correct |
124 ms |
37120 KB |
Output is correct |
5 |
Correct |
130 ms |
32608 KB |
Output is correct |
6 |
Correct |
176 ms |
24592 KB |
Output is correct |
7 |
Correct |
146 ms |
37228 KB |
Output is correct |
8 |
Correct |
137 ms |
37220 KB |
Output is correct |
9 |
Correct |
100 ms |
36248 KB |
Output is correct |
10 |
Correct |
119 ms |
27220 KB |
Output is correct |
11 |
Correct |
133 ms |
25676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
174 ms |
23304 KB |
Output is correct |
3 |
Correct |
184 ms |
25840 KB |
Output is correct |
4 |
Correct |
213 ms |
24072 KB |
Output is correct |
5 |
Correct |
149 ms |
27204 KB |
Output is correct |
6 |
Correct |
139 ms |
26196 KB |
Output is correct |
7 |
Correct |
111 ms |
32076 KB |
Output is correct |
8 |
Correct |
98 ms |
38392 KB |
Output is correct |
9 |
Correct |
117 ms |
14384 KB |
Output is correct |
10 |
Correct |
149 ms |
23988 KB |
Output is correct |
11 |
Correct |
148 ms |
24796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
560 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
159 ms |
28644 KB |
Output is correct |
18 |
Correct |
117 ms |
35352 KB |
Output is correct |
19 |
Correct |
158 ms |
23440 KB |
Output is correct |
20 |
Correct |
147 ms |
24628 KB |
Output is correct |
21 |
Correct |
152 ms |
25292 KB |
Output is correct |
22 |
Correct |
146 ms |
37804 KB |
Output is correct |
23 |
Correct |
137 ms |
24460 KB |
Output is correct |
24 |
Correct |
160 ms |
26520 KB |
Output is correct |
25 |
Correct |
115 ms |
26268 KB |
Output is correct |
26 |
Correct |
151 ms |
25920 KB |
Output is correct |
27 |
Correct |
124 ms |
36360 KB |
Output is correct |
28 |
Correct |
110 ms |
36144 KB |
Output is correct |
29 |
Correct |
128 ms |
38580 KB |
Output is correct |
30 |
Correct |
98 ms |
37296 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
212 ms |
22444 KB |
Output is correct |
33 |
Correct |
180 ms |
25080 KB |
Output is correct |
34 |
Correct |
124 ms |
37120 KB |
Output is correct |
35 |
Correct |
130 ms |
32608 KB |
Output is correct |
36 |
Correct |
176 ms |
24592 KB |
Output is correct |
37 |
Correct |
146 ms |
37228 KB |
Output is correct |
38 |
Correct |
137 ms |
37220 KB |
Output is correct |
39 |
Correct |
100 ms |
36248 KB |
Output is correct |
40 |
Correct |
119 ms |
27220 KB |
Output is correct |
41 |
Correct |
133 ms |
25676 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
174 ms |
23304 KB |
Output is correct |
44 |
Correct |
184 ms |
25840 KB |
Output is correct |
45 |
Correct |
213 ms |
24072 KB |
Output is correct |
46 |
Correct |
149 ms |
27204 KB |
Output is correct |
47 |
Correct |
139 ms |
26196 KB |
Output is correct |
48 |
Correct |
111 ms |
32076 KB |
Output is correct |
49 |
Correct |
98 ms |
38392 KB |
Output is correct |
50 |
Correct |
117 ms |
14384 KB |
Output is correct |
51 |
Correct |
149 ms |
23988 KB |
Output is correct |
52 |
Correct |
148 ms |
24796 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
564 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
596 KB |
Output is correct |
62 |
Correct |
2 ms |
596 KB |
Output is correct |
63 |
Correct |
1 ms |
560 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
1 ms |
700 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
155 ms |
30508 KB |
Output is correct |
68 |
Correct |
136 ms |
35840 KB |
Output is correct |
69 |
Correct |
149 ms |
22916 KB |
Output is correct |
70 |
Correct |
153 ms |
24576 KB |
Output is correct |
71 |
Correct |
180 ms |
24168 KB |
Output is correct |
72 |
Correct |
119 ms |
37376 KB |
Output is correct |
73 |
Correct |
132 ms |
24548 KB |
Output is correct |
74 |
Correct |
125 ms |
24648 KB |
Output is correct |
75 |
Correct |
139 ms |
24956 KB |
Output is correct |
76 |
Correct |
149 ms |
25252 KB |
Output is correct |
77 |
Correct |
112 ms |
36152 KB |
Output is correct |
78 |
Correct |
124 ms |
35020 KB |
Output is correct |
79 |
Correct |
131 ms |
37428 KB |
Output is correct |
80 |
Correct |
93 ms |
38264 KB |
Output is correct |
81 |
Correct |
183 ms |
24764 KB |
Output is correct |
82 |
Correct |
223 ms |
25396 KB |
Output is correct |
83 |
Correct |
107 ms |
35188 KB |
Output is correct |
84 |
Correct |
110 ms |
33172 KB |
Output is correct |
85 |
Correct |
206 ms |
27024 KB |
Output is correct |
86 |
Correct |
130 ms |
39476 KB |
Output is correct |
87 |
Correct |
122 ms |
39372 KB |
Output is correct |
88 |
Correct |
111 ms |
26636 KB |
Output is correct |
89 |
Correct |
115 ms |
23340 KB |
Output is correct |
90 |
Correct |
235 ms |
25804 KB |
Output is correct |
91 |
Correct |
171 ms |
27152 KB |
Output is correct |
92 |
Correct |
175 ms |
27168 KB |
Output is correct |
93 |
Correct |
148 ms |
26276 KB |
Output is correct |
94 |
Correct |
143 ms |
26480 KB |
Output is correct |
95 |
Correct |
123 ms |
33928 KB |
Output is correct |
96 |
Correct |
113 ms |
37820 KB |
Output is correct |
97 |
Correct |
97 ms |
14792 KB |
Output is correct |
98 |
Correct |
131 ms |
25632 KB |
Output is correct |
99 |
Correct |
133 ms |
26112 KB |
Output is correct |
100 |
Correct |
49 ms |
4940 KB |
Output is correct |
101 |
Correct |
225 ms |
27100 KB |
Output is correct |
102 |
Correct |
138 ms |
23724 KB |
Output is correct |
103 |
Correct |
175 ms |
22312 KB |
Output is correct |
104 |
Correct |
220 ms |
23896 KB |
Output is correct |