Submission #666436

# Submission time Handle Problem Language Result Execution time Memory
666436 2022-11-28T15:53:41 Z rainboy Meetings 2 (JOI21_meetings2) C
0 / 100
4000 ms 212 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 212 KB Output is correct
3 Execution timed out 4041 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Execution timed out 4041 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Execution timed out 4041 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -