#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
unsigned int X;
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;
}
}
int 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Execution timed out |
1048 ms |
2872 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
210 ms |
25068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
180 ms |
24436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |