답안 #482431

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482431 2021-10-24T14:36:33 Z rainboy Papričice (COCI20_papricice) C
110 / 110
156 ms 20420 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	200000

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

int dist(int a, int b, int c) {
	return max(max(a, b), c) - min(min(a, b), c);
}

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 ta[N], tb[N], c;
int rr[N], sz[N];

void dfs1(int p, int i) {
	static int time;
	int o, centroid;

	ta[i] = time++;
	sz[i] = 1, centroid = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs1(i, j);
			sz[i] += sz[j];
			if (sz[j] * 2 > n)
				centroid = 0;
		}
	}
	if ((n - sz[i]) * 2 > n)
		centroid = 0;
	if (centroid)
		c = i;
	tb[i] = time;
}

void dfs2(int p, int r, int i) {
	int o;

	rr[i] = r, sz[i] = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs2(i, r, j);
			sz[i] += sz[j];
		}
	}
}

int main() {
	static int rr_[N], dp1[N], dp2[N], dq1[N], dq2[N];
	int h, i, j, s, o, ans;

	scanf("%d", &n);
	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);
	}
	dfs1(-1, 0);
	for (o = eo[c]; o--; ) {
		j = ej[c][o];
		dfs2(c, j, j);
	}
	memset(rr_, -1, n * sizeof *rr_);
	for (i = 0; i < n; i++)
		if (i != c) {
			if (rr_[sz[i]] == -1)
				rr_[sz[i]] = rr[i];
			else if (rr_[sz[i]] != rr[i])
				rr_[sz[i]] = -2;
		}
	for (s = 0; s < n; s++) {
		dp1[s] = s == 0 ? -1 : dp1[s - 1], dp2[s] = s == 0 ? -1 : dp2[s - 1];
		if (rr_[s] != -1) {
			if (dp1[s] == -1 || rr_[dp1[s]] != rr_[s])
				dp2[s] = dp1[s];
			dp1[s] = s;
		}
	}
	for (s = n - 1; s >= 0; s--) {
		dq1[s] = s + 1 == n ? n : dq1[s + 1], dq2[s] = s + 1 == n ? n : dq2[s + 1];
		if (rr_[s] != -1) {
			if (dq1[s] == n || rr_[dq1[s]] != rr_[s])
				dq2[s] = dq1[s];
			dq1[s] = s;
		}
	}
	ans = n + 1;
	for (i = 0; i < n; i++)
		if (i != c) {
			int s, s_;

			if (i != rr[i])
				ans = min(ans, dist(n - sz[rr[i]], sz[rr[i]] - sz[i], sz[i]));
			s = (n - sz[i]) / 2;
			if ((s_ = dp1[s]) != -1 && rr_[s_] != rr[i])
				ans = min(ans, dist(sz[i], s_, n - sz[i] - s_));
			else if ((s_ = dp2[s]) != -1)
				ans = min(ans, dist(sz[i], s_, n - sz[i] - s_));
			if ((s_ = dq1[s]) != n && rr_[s_] != rr[i])
				ans = min(ans, dist(sz[i], s_, n - sz[i] - s_));
			else if ((s_ = dq2[s]) != n)
				ans = min(ans, dist(sz[i], s_, n - sz[i] - s_));
		}
	printf("%d\n", ans);
	return 0;
}

Compilation message

papricice.c: In function 'append':
papricice.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
papricice.c: In function 'main':
papricice.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
papricice.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 146 ms 16620 KB Output is correct
12 Correct 152 ms 16064 KB Output is correct
13 Correct 118 ms 16836 KB Output is correct
14 Correct 156 ms 16592 KB Output is correct
15 Correct 140 ms 16184 KB Output is correct
16 Correct 97 ms 16904 KB Output is correct
17 Correct 120 ms 16312 KB Output is correct
18 Correct 130 ms 20420 KB Output is correct
19 Correct 120 ms 16636 KB Output is correct
20 Correct 119 ms 16064 KB Output is correct