Submission #439810

# Submission time Handle Problem Language Result Execution time Memory
439810 2021-06-30T20:13:25 Z rainboy Mag (COCI16_mag) C
120 / 120
513 ms 159044 KB
#include <stdio.h>
#include <stdlib.h>

#define N	1000000
#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; }

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 aa[N], kk[N];

void dfs1(int p, int i) {
	int o, k;

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

		if (j != p) {
			dfs1(i, j);
			k = max(k, kk[j]);
		}
	}
	kk[i] = aa[i] == 1 ? k + 1 : 0;
}

int p_, q_;

void dfs2(int p, int i, int k) {
	int o, k1, k2, j1;

	if (aa[i] == 1) {
		q_ = max(q_, kk[i] = max(kk[i], k + 1));
		k1 = k2 = k, j1 = -1;
		for (o = eo[i]; o--; ) {
			int j = ej[i][o];

			if (j != p) {
				if (k1 < kk[j])
					k2 = k1, k1 = kk[j], j1 = j;
				else if (k2 < kk[j])
					k2 = kk[j];
			}
		}
		k1++, k2++;
	} else
		k1 = k2 = 0, j1 = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs2(i, j, j == j1 ? k2 : k1);
	}
}

int main() {
	int n, h, i, j;

	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);
	}
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	p_ = 1;
	dfs1(-1, 0), dfs2(-1, 0, 0);
	for (i = 0; i < n; i++)
		if (aa[i] > 1) {
			int o, k1, k2;

			k1 = k2 = 0;
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				if (k1 < kk[j])
					k2 = k1, k1 = kk[j];
				else if (k2 < kk[j])
					k2 = kk[j];
			}
			if ((long long) p_ * (k1 + k2 + 1) > (long long) q_ * aa[i])
				p_ = aa[i], q_ = k1 + k2 + 1;
		}
	printf("%d/%d\n", p_, q_);
	return 0;
}

Compilation message

mag.c: In function 'append':
mag.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
mag.c: In function 'main':
mag.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
mag.c:73:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
mag.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 78536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 513 ms 159044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 481 ms 141464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 408 ms 50884 KB Output is correct
2 Correct 299 ms 37568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 410 ms 51908 KB Output is correct
2 Correct 62 ms 5572 KB Output is correct
3 Correct 505 ms 145008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 5572 KB Output is correct
2 Correct 415 ms 51492 KB Output is correct
3 Correct 362 ms 27180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 381 ms 48008 KB Output is correct
2 Correct 407 ms 50060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 411 ms 51596 KB Output is correct
2 Correct 347 ms 27236 KB Output is correct