#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 150000
#define M 200000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *ej[N], eo[N], n;
void append(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 aa[N * 2], ii[N * 2];
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 ds[N];
int find(int i) {
return ds[i] < 0 ? i : find(ds[i]);
}
int ii1[M], jj1[M], xx[M], cnt;
void join(int i, int j) {
int tmp;
j = find(j);
i = find(i);
if (i == j) {
ii1[cnt] = -1, jj1[cnt] = -1, xx[cnt] = 0, cnt++;
return;
}
if (ds[i] > ds[j])
tmp = i, i = j, j = tmp;
ii1[cnt] = i, jj1[cnt] = j, xx[cnt] = ds[j], cnt++;
ds[i] += ds[j], ds[j] = i;
}
void undo() {
int i, j, x;
cnt--;
i = ii1[cnt], j = jj1[cnt], x = xx[cnt];
if (i == -1)
return;
ds[j] = x, ds[i] -= ds[j];
}
char active[N];
void activate(int i) {
int o;
active[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (active[j])
join(i, j);
}
}
void deactivate(int i) {
int o;
active[i] = 0;
for (o = 0; o < eo[i]; o++) {
int j = ej[i][o];
if (active[j])
undo();
}
}
int i_, j_;
int dfs(int *ii_, int m, int al, int ar) {
static char used[N];
int am, h, h1, h2, h3, i, good, tmp;
h1 = 0, h2 = 0, h3 = m;
while (h2 < h3)
if (aa[ii_[h2] << 1 | 0] <= al && ar <= aa[ii_[h2] << 1 | 1])
h2++;
else if (aa[ii_[h2] << 1 | 0] <= ar && al <= aa[ii_[h2] << 1 | 1]) {
tmp = ii_[h1], ii_[h1] = ii_[h2], ii_[h2] = tmp;
h1++, h2++;
} else {
h3--;
tmp = ii_[h2], ii_[h2] = ii_[h3], ii_[h3] = tmp;
}
for (h = h1; h < h2; h++)
activate(ii_[h]);
if (al < ar) {
am = (al + ar) / 2;
good = dfs(ii_, h1, al, am) && dfs(ii_, h1, am + 1, ar);
} else {
j_ = i_;
while (j_ < n * 2 && aa[ii[j_]] == al)
j_++;
for (h = i_; h < j_; h++) {
i = ii[h];
if ((i & 1) == 1)
used[find(i >> 1)] = 1;
}
good = 1;
for (h = i_; h < j_; h++) {
i = ii[h];
if ((i & 1) == 0 && !used[find(i >> 1)])
good = 0;
}
for (h = i_; h < j_; h++) {
i = ii[h];
if ((i & 1) == 1)
used[find(i >> 1)] = 0;
}
i_ = j_;
}
for (h = h2 - 1; h >= h1; h--)
deactivate(ii_[h]);
return good;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static int ii_[N];
int m, h, i, j, yes;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--;
for (i = 0; i < n; i++)
scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--;
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
yes = 1;
for (i = 0; i < n; i++)
if (aa[i << 1 | 0] > aa[i << 1 | 1])
yes = 0;
if (!yes) {
printf("0\n");
for (i = 0; i < n; i++)
free(ej[i]);
continue;
}
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2);
memset(active, 0, n * sizeof *active), memset(ds, -1, n * sizeof *ds);
for (i = 0; i < n; i++)
ii_[i] = i;
i_ = 0;
printf("%d\n", dfs(ii_, n, 0, n - 1));
for (i = 0; i < n; i++)
free(ej[i]);
}
return 0;
}
Compilation message
colors.c: In function 'append':
colors.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
colors.c: In function 'main':
colors.c:156:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | scanf("%d", &t);
| ^~~~~~~~~~~~~~~
colors.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
colors.c:163:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:165:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
165 | scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:169:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
476 KB |
Output is correct |
2 |
Correct |
31 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
1564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
460 KB |
Output is correct |
2 |
Correct |
34 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
460 KB |
Output is correct |
2 |
Correct |
34 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
189 ms |
344 KB |
Output is correct |
5 |
Correct |
411 ms |
3984 KB |
Output is correct |
6 |
Correct |
666 ms |
11744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
476 KB |
Output is correct |
2 |
Correct |
31 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
95 ms |
460 KB |
Output is correct |
5 |
Correct |
34 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
7 |
Correct |
103 ms |
344 KB |
Output is correct |
8 |
Correct |
34 ms |
356 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
348 KB |
Output is correct |
2 |
Correct |
364 ms |
4316 KB |
Output is correct |
3 |
Correct |
446 ms |
11580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
1096 KB |
Output is correct |
2 |
Correct |
22 ms |
724 KB |
Output is correct |
3 |
Correct |
9 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
476 KB |
Output is correct |
2 |
Correct |
31 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
89 ms |
1564 KB |
Output is correct |
5 |
Correct |
95 ms |
460 KB |
Output is correct |
6 |
Correct |
34 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
189 ms |
344 KB |
Output is correct |
9 |
Correct |
411 ms |
3984 KB |
Output is correct |
10 |
Correct |
666 ms |
11744 KB |
Output is correct |
11 |
Correct |
103 ms |
344 KB |
Output is correct |
12 |
Correct |
34 ms |
356 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
180 ms |
348 KB |
Output is correct |
15 |
Correct |
364 ms |
4316 KB |
Output is correct |
16 |
Correct |
446 ms |
11580 KB |
Output is correct |
17 |
Correct |
58 ms |
1096 KB |
Output is correct |
18 |
Correct |
22 ms |
724 KB |
Output is correct |
19 |
Correct |
9 ms |
472 KB |
Output is correct |
20 |
Correct |
121 ms |
2860 KB |
Output is correct |
21 |
Correct |
470 ms |
12540 KB |
Output is correct |
22 |
Correct |
712 ms |
20448 KB |
Output is correct |