답안 #488548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
488548 2021-11-19T15:03:47 Z rainboy Logičari (COCI21_logicari) C
110 / 110
53 ms 8136 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define INF	0x3f3f3f3f

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

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

void dfs(int i) {
	int o, c, d;

	if (dd[i] == eo[i] - 1) {
		dd[i] = -1;
		for (o = eo[i]; o--; ) {
			int j = ej[i][o];

			if (dd[j] != -1) {
				for (c = 0; c < 2; c++)
					for (d = 1; d >= 0; d--) {
						int x = INF;

						if (dp[i][0][1 - c] != INF)
							x = min(x, dp[j][c][d] + dp[i][0][1 - c]);
						if (d == 1 && dp[i][1][1 - c] != INF)
							x = min(x, dp[j][c][0] + dp[i][1][1 - c]);
						dp[j][c][d] = x;
					}
				dd[j]++, dfs(j);
				break;
			}
		}
	}
}

int main() {
	static int qu[N], dp_[2][2][2][2], dq_[2][2][2][2];
	int n, h, i, j, o, cnt, c1, d1, c2, d2, c3, d3, ans;

	scanf("%d", &n);
	if (n > N) {
		printf("%d\n", n % 4 == 0 ? n / 2 : -1);
		return 0;
	}
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	for (i = 0; i < n; i++)
		dp[i][0][0] = 0, dp[i][0][1] = INF, dp[i][1][0] = 1, dp[i][1][1] = INF;
	for (i = 0; i < n; i++)
		dfs(i);
	for (i = 0; i < n; i++)
		if (dd[i] != -1)
			break;
	cnt = 0;
	while (dd[i] != -1) {
		dd[i] = -1, qu[cnt++] = i;
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (dd[j] != -1) {
				i = j;
				break;
			}
		}
	}
	for (c1 = 0; c1 < 2; c1++)
		for (d1 = 0; d1 < 2; d1++)
			for (c2 = 0; c2 < 2; c2++)
				for (d2 = 0; d2 < 2; d2++)
					dp_[c1][d1][c2][d2] = INF;
	for (c1 = 0; c1 < 2; c1++)
		for (d1 = 0; d1 < 2; d1++)
			for (c2 = 0; c2 < 2; c2++)
				for (d2 = 0; d2 < 2; d2++) {
					if (d1 + c2 > 1 || d2 + c1 > 1)
						continue;
					dp_[c1][d1 + c2][c2][d2 + c1] = min(dp_[c1][d1 + c2][c2][d2 + c1], dp[qu[0]][c1][d1] + dp[qu[1]][c2][d2]);
				}
	for (h = 2; h < cnt; h++) {
		i = qu[h];
		for (c1 = 0; c1 < 2; c1++)
			for (d1 = 0; d1 < 2; d1++)
				for (c2 = 0; c2 < 2; c2++)
					for (d2 = 0; d2 < 2; d2++)
						dq_[c1][d1][c2][d2] = INF;
		for (c1 = 0; c1 < 2; c1++)
			for (d1 = 0; d1 < 2; d1++)
				for (c2 = 0; c2 < 2; c2++)
					for (d2 = 0; d2 < 2; d2++) {
						int x = dp_[c1][d1][c2][d2];

						if (x == INF)
							continue;
						for (c3 = 0; c3 < 2; c3++)
							for (d3 = 0; d3 < 2; d3++) {
								int y = dp[i][c3][d3];

								if (d2 + c3 != 1 || d3 + c2 > 1 || y == INF)
									continue;
								dq_[c1][d1][c3][d3 + c2] = min(dq_[c1][d1][c3][d3 + c2], x + y);
							}
					}
		for (c1 = 0; c1 < 2; c1++)
			for (d1 = 0; d1 < 2; d1++)
				for (c2 = 0; c2 < 2; c2++)
					for (d2 = 0; d2 < 2; d2++)
						dp_[c1][d1][c2][d2] = dq_[c1][d1][c2][d2];
	}
	ans = INF;
	for (c1 = 0; c1 < 2; c1++)
		for (d1 = 0; d1 < 2; d1++)
			for (c2 = 0; c2 < 2; c2++)
				for (d2 = 0; d2 < 2; d2++)
					if (d1 + c2 == 1 && d2 + c1 == 1)
						ans = min(ans, dp_[c1][d1][c2][d2]);
	if (ans == INF)
		ans = -1;
	printf("%d\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:51:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:59:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |   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 42 ms 7976 KB Output is correct
6 Correct 49 ms 7968 KB Output is correct
7 Correct 53 ms 8100 KB Output is correct
8 Correct 53 ms 7984 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 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 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 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 324 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 332 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 42 ms 7976 KB Output is correct
6 Correct 49 ms 7968 KB Output is correct
7 Correct 53 ms 8100 KB Output is correct
8 Correct 53 ms 7984 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 324 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 40 ms 6904 KB Output is correct
34 Correct 37 ms 6856 KB Output is correct
35 Correct 34 ms 6952 KB Output is correct
36 Correct 42 ms 6860 KB Output is correct
37 Correct 8 ms 2000 KB Output is correct
38 Correct 43 ms 7116 KB Output is correct
39 Correct 3 ms 848 KB Output is correct
40 Correct 43 ms 7116 KB Output is correct
41 Correct 35 ms 8068 KB Output is correct
42 Correct 47 ms 8136 KB Output is correct
43 Correct 36 ms 7632 KB Output is correct
44 Correct 34 ms 7924 KB Output is correct