답안 #482871

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482871 2021-10-26T16:07:25 Z rainboy Svjetlo (COCI20_svjetlo) C
110 / 110
592 ms 113360 KB
#include <stdio.h>
#include <stdlib.h>

#define N	500000
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }

int *ej[N], eo[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;
}

char cc[N + 1], empty[N];
int dp[N][3][3];

void dfs(int p, int i) {
	static int dq[3][3][2], dr[3][3][2];
	int o, k, e, c, k1, e1, c1, k2, e2, j_;

	empty[i] = cc[i] == '1';
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs(i, j);
			if (!empty[j])
				empty[i] = 0;
		}
	}
	for (k = 0; k <= 2; k++)
		for (e = 0; e <= 2; e++)
			for (c = 0; c < 2; c++)
				dq[k][e][c] = INF;
	c = cc[i] - '0';
	dq[0][0][c] = 0, dq[0][1][c ^ 1] = 2, dq[0][2][c] = 4;
	dq[1][0][c ^ 1] = 1, dq[1][1][c] = 3, dq[1][2][c ^ 1] = 5;
	dq[2][0][c ^ 1] = 1, dq[2][1][c] = 3, dq[2][2][c ^ 1] = 5;
	j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			if (!empty[j]) {
				for (k = 0; k <= 2; k++)
					for (e = 0; e <= 2; e++)
						for (c = 0; c < 2; c++)
							dr[k][e][c] = INF;
				j_ = j_ == -1 ? j : -2;
			} else {
				for (k = 0; k <= 2; k++)
					for (e = 0; e <= 2; e++)
						for (c = 0; c < 2; c++)
							dr[k][e][c] = dq[k][e][c];
			}
			for (k1 = 0; k1 <= 2; k1++)
				for (e1 = 0; e1 <= 2; e1++)
					for (c1 = 0; c1 < 2; c1++)
						for (k2 = 0; k1 + k2 <= 2; k2++)
							for (e2 = 0; e2 <= 2; e2++) {
								int x = dq[k1][e1][c1] + dp[j][k2][e2];

								if (k2 != 1 && e2 == 0)
									continue;
								k = k1 + k2, e = e1, c = c1 ^ (e2 & 1);
								if (k1 == 0 && k2 == 1)
									x++, c ^= 1;
								dr[k][e][c] = min(dr[k][e][c], x);
							}
			for (k = 0; k <= 2; k++)
				for (e = 0; e <= 2; e++)
					for (c = 0; c < 2; c++)
						dq[k][e][c] = dr[k][e][c];
		}
	}
	c = cc[i] - '0';
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && (j_ == -1 || j == j_))
			dq[0][0][c] = min(dq[0][0][c], dp[j][0][0]), dq[2][0][c] = min(dq[2][0][c], dp[j][2][0]);
	}
	c = 1;
	for (k = 0; k <= 2; k++)
		for (e = 0; e <= 2; e++)
			dp[i][k][e] = dq[k][e][c];
}

int main() {
	int n, h, i, j;

	scanf("%d%s", &n, cc);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	dfs(-1, 0);
	printf("%d\n", dp[0][2][0]);
	return 0;
}

Compilation message

svjetlo.c: In function 'append':
svjetlo.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
svjetlo.c: In function 'main':
svjetlo.c:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |  scanf("%d%s", &n, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~
svjetlo.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 396 ms 60156 KB Output is correct
2 Correct 586 ms 101064 KB Output is correct
3 Correct 589 ms 113360 KB Output is correct
4 Correct 497 ms 76800 KB Output is correct
5 Correct 592 ms 90656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 360 ms 31992 KB Output is correct
2 Correct 452 ms 39436 KB Output is correct
3 Correct 558 ms 40032 KB Output is correct
4 Correct 406 ms 33852 KB Output is correct
5 Correct 427 ms 39660 KB Output is correct
6 Correct 373 ms 34532 KB Output is correct
7 Correct 459 ms 39612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 396 ms 60156 KB Output is correct
11 Correct 586 ms 101064 KB Output is correct
12 Correct 589 ms 113360 KB Output is correct
13 Correct 497 ms 76800 KB Output is correct
14 Correct 592 ms 90656 KB Output is correct
15 Correct 360 ms 31992 KB Output is correct
16 Correct 452 ms 39436 KB Output is correct
17 Correct 558 ms 40032 KB Output is correct
18 Correct 406 ms 33852 KB Output is correct
19 Correct 427 ms 39660 KB Output is correct
20 Correct 373 ms 34532 KB Output is correct
21 Correct 459 ms 39612 KB Output is correct
22 Correct 378 ms 41004 KB Output is correct
23 Correct 419 ms 43460 KB Output is correct
24 Correct 385 ms 41548 KB Output is correct
25 Correct 458 ms 39184 KB Output is correct
26 Correct 462 ms 48140 KB Output is correct
27 Correct 455 ms 48992 KB Output is correct
28 Correct 405 ms 42368 KB Output is correct
29 Correct 469 ms 45612 KB Output is correct
30 Correct 488 ms 42496 KB Output is correct