답안 #666433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
666433 2022-11-28T15:51:26 Z rainboy Meetings 2 (JOI21_meetings2) C
20 / 100
282 ms 25000 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

int *ej[N], eo[N], *ej_[N], eo_[N];

void append(int **ej, int *eo, 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 pp[N], dd[N], qq[N], sz[N];

void dfs1(int p, int i, int d) {
	int o, j_;

	pp[i] = p, dd[i] = d, sz[i] = 1, j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs1(i, j, d + 1);
			sz[i] += sz[j];
			if (j_ == -1 || sz[j_] < sz[j])
				j_ = j;
		}
	}
	qq[i] = j_;
}

void dfs2(int p, int i, int q) {
	int o, j_;

	j_ = qq[i], qq[i] = q;
	if (j_ == -1)
		dfs2(i, j_, q);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs2(i, j, j);
	}
}

int lca(int i, int j) {
	while (qq[i] != qq[j])
		if (dd[qq[i]] > dd[qq[j]])
			i = pp[qq[i]];
		else
			j = pp[qq[j]];
	return dd[i] < dd[j] ? i : j;
}

int dist(int i, int j) {
	return dd[i] + dd[j] - dd[lca(i, j)] * 2;
}

int main() {
	static char used[N];
	static int ans[N + 1];
	int n, h, i, j, k, u, v, o, d;

	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(ej, eo, i, j), append(ej, eo, j, i);
	}
	dfs1(-1, 0, 0), dfs2(-1, 0, 0);
	for (i = 0; i < n; i++)
		ej_[i] = (int *) malloc(2 * sizeof *ej_[i]);
	for (i = 1; i < n; i++)
		if (sz[i] < n - sz[i])
			used[i] = 1, append(ej_, eo_, sz[i], i);
		else if (sz[i] > n - sz[i])
			used[pp[i]] = 1, append(ej_, eo_, n - sz[i], pp[i]);
	u = v = -1;
	for (i = 0; i < n; i++)
		if (!used[i]) {
			if (u == -1)
				u = i;
			else
				v = i;
		}
	if (v == -1)
		v = u;
	d = dist(u, v);
	for (k = n; k >= 1; k--)
		if (k % 2 != 0)
			ans[k] = 1;
		else {
			for (o = eo_[k / 2]; o--; ) {
				int du, dv;

				j = ej_[k / 2][o];
				du = dist(u, j), dv = dist(v, j);
				if (du >= d && du >= dv)
					v = j, d = du;
				else if (dv >= d && dv >= du)
					u = j, d = dv;
			}
			ans[k] = d + 1;
		}
	for (k = 1; k <= n; k++)
		printf("%d\n", ans[k]);
	return 0;
}

Compilation message

meetings2.c: In function 'append':
meetings2.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   11 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
meetings2.c: In function 'main':
meetings2.c:67:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
meetings2.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 288 KB Output is correct
20 Correct 1 ms 292 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 288 KB Output is correct
20 Correct 1 ms 292 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 3 ms 816 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 3 ms 776 KB Output is correct
25 Correct 3 ms 820 KB Output is correct
26 Correct 3 ms 816 KB Output is correct
27 Correct 3 ms 776 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 772 KB Output is correct
30 Correct 3 ms 816 KB Output is correct
31 Correct 3 ms 772 KB Output is correct
32 Correct 39 ms 924 KB Output is correct
33 Correct 42 ms 1000 KB Output is correct
34 Correct 2 ms 820 KB Output is correct
35 Correct 2 ms 816 KB Output is correct
36 Correct 5 ms 768 KB Output is correct
37 Correct 3 ms 780 KB Output is correct
38 Correct 43 ms 888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 288 KB Output is correct
20 Correct 1 ms 292 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 3 ms 816 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 3 ms 776 KB Output is correct
25 Correct 3 ms 820 KB Output is correct
26 Correct 3 ms 816 KB Output is correct
27 Correct 3 ms 776 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 772 KB Output is correct
30 Correct 3 ms 816 KB Output is correct
31 Correct 3 ms 772 KB Output is correct
32 Correct 39 ms 924 KB Output is correct
33 Correct 42 ms 1000 KB Output is correct
34 Correct 2 ms 820 KB Output is correct
35 Correct 2 ms 816 KB Output is correct
36 Correct 5 ms 768 KB Output is correct
37 Correct 3 ms 780 KB Output is correct
38 Correct 43 ms 888 KB Output is correct
39 Correct 246 ms 24600 KB Output is correct
40 Correct 250 ms 23968 KB Output is correct
41 Correct 282 ms 24616 KB Output is correct
42 Incorrect 261 ms 25000 KB Output isn't correct
43 Halted 0 ms 0 KB -