답안 #739036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739036 2023-05-09T19:14:12 Z rainboy Colors (RMI18_colors) C
0 / 100
3000 ms 340 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_;
		return good;
	}
	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:153:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
colors.c:158:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |   scanf("%d%d", &n, &m);
      |   ^~~~~~~~~~~~~~~~~~~~~
colors.c:160:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |    scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:162:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |    scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:166:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |    scanf("%d%d", &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3076 ms 312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3072 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3075 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3075 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3076 ms 312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3058 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3051 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3076 ms 312 KB Time limit exceeded
2 Halted 0 ms 0 KB -