답안 #739039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739039 2023-05-09T19:23:32 Z rainboy Colors (RMI18_colors) C
61 / 100
3000 ms 13088 KB
#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)
		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];
	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:152:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
colors.c:157:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |   scanf("%d%d", &n, &m);
      |   ^~~~~~~~~~~~~~~~~~~~~
colors.c:159:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |    scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:161:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |    scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:165:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |    scanf("%d%d", &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 1696 KB Output is correct
2 Correct 32 ms 844 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3027 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 1512 KB Output is correct
2 Correct 32 ms 852 KB Output is correct
3 Correct 4 ms 432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 1512 KB Output is correct
2 Correct 32 ms 852 KB Output is correct
3 Correct 4 ms 432 KB Output is correct
4 Correct 199 ms 1528 KB Output is correct
5 Correct 427 ms 5352 KB Output is correct
6 Correct 699 ms 13088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 1696 KB Output is correct
2 Correct 32 ms 844 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 121 ms 1512 KB Output is correct
5 Correct 32 ms 852 KB Output is correct
6 Correct 4 ms 432 KB Output is correct
7 Correct 107 ms 1568 KB Output is correct
8 Correct 38 ms 852 KB Output is correct
9 Correct 5 ms 436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 1492 KB Output is correct
2 Correct 401 ms 5608 KB Output is correct
3 Correct 447 ms 12896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3053 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 1696 KB Output is correct
2 Correct 32 ms 844 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Execution timed out 3027 ms 340 KB Time limit exceeded
5 Halted 0 ms 0 KB -