Submission #486453

# Submission time Handle Problem Language Result Execution time Memory
486453 2021-11-11T17:17:20 Z rainboy Torrent (COI16_torrent) C
100 / 100
288 ms 23644 KB
#include <stdio.h>
#include <stdlib.h>

#define N	300000
#define INF	0x3f3f3f3f

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

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int *ej[N], eo[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 qu[N], cnt;

int dfs1(int p, int i, int t) {
	int o;

	if (i == t) {
		qu[cnt++] = i;
		return 1;
	}
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && dfs1(i, j, t)) {
			qu[cnt++] = i;
			return 1;
		}
	}
	return 0;
}

int dp[N], w, x;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (dp[ii[j]] == dp[i_])
				j++;
			else if (dp[ii[j]] > dp[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

void dfs2(int p, int i) {
	static int jj[N];
	int o, cnt, h;

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

		if (j != p) {
			if (i == w && j == x || i == x && j == w)
				continue;
			dfs2(i, j);
		}
	}
	cnt = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			if (i == w && j == x || i == x && j == w)
				continue;
			jj[cnt++] = j;
		}
	}
	sort(jj, 0, cnt);
	dp[i] = 0;
	for (h = 0; h < cnt; h++)
		dp[i] = max(dp[i], dp[jj[h]] + h + 1);
}

int main() {
	int n, h, i, j, u, v, ans, lower, upper;

	scanf("%d%d%d", &n, &u, &v), u--, v--;
	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, u, v);
	ans = INF;
	lower = -1, upper = cnt - 1;
	while (upper - lower > 1) {
		int h = (lower + upper) / 2;

		w = qu[h + 1], x = qu[h];
		dfs2(-1, u), dfs2(-1, v);
		ans = min(ans, max(dp[u], dp[v]));
		if (dp[u] > dp[v])
			lower = h;
		else
			upper = h;
	}
	printf("%d\n", ans);
	return 0;
}

Compilation message

torrent.c: In function 'append':
torrent.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
torrent.c: In function 'dfs2':
torrent.c:75:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   75 |    if (i == w && j == x || i == x && j == w)
      |        ~~~~~~~^~~~~~~~~
torrent.c:85:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   85 |    if (i == w && j == x || i == x && j == w)
      |        ~~~~~~~^~~~~~~~~
torrent.c: In function 'main':
torrent.c:99:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |  scanf("%d%d%d", &n, &u, &v), u--, v--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
torrent.c:103:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 16632 KB Output is correct
2 Correct 288 ms 21808 KB Output is correct
3 Correct 248 ms 23404 KB Output is correct
4 Correct 259 ms 22296 KB Output is correct
5 Correct 250 ms 20360 KB Output is correct
6 Correct 243 ms 21056 KB Output is correct
7 Correct 223 ms 23644 KB Output is correct