Submission #666439

# Submission time Handle Problem Language Result Execution time Memory
666439 2022-11-28T15:55:11 Z rainboy Meetings 2 (JOI21_meetings2) C
100 / 100
245 ms 33280 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 && j != j_)
			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--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 3 ms 724 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 3 ms 724 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 4 ms 724 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 3 ms 852 KB Output is correct
33 Correct 3 ms 936 KB Output is correct
34 Correct 3 ms 724 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 2 ms 740 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 3 ms 724 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 3 ms 724 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 4 ms 724 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 3 ms 852 KB Output is correct
33 Correct 3 ms 936 KB Output is correct
34 Correct 3 ms 724 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 2 ms 740 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
39 Correct 186 ms 22108 KB Output is correct
40 Correct 169 ms 21592 KB Output is correct
41 Correct 170 ms 22132 KB Output is correct
42 Correct 171 ms 22396 KB Output is correct
43 Correct 195 ms 25020 KB Output is correct
44 Correct 207 ms 25008 KB Output is correct
45 Correct 245 ms 29732 KB Output is correct
46 Correct 150 ms 33280 KB Output is correct
47 Correct 126 ms 25188 KB Output is correct
48 Correct 156 ms 25304 KB Output is correct
49 Correct 137 ms 25976 KB Output is correct
50 Correct 141 ms 25368 KB Output is correct
51 Correct 150 ms 32536 KB Output is correct