Submission #439807

# Submission time Handle Problem Language Result Execution time Memory
439807 2021-06-30T20:07:28 Z rainboy Mag (COCI16_mag) C
24 / 120
538 ms 144976 KB
#include <stdio.h>
#include <stdlib.h>

#define N	1000000

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] = 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 gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	int n, h, i, j, 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(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;
		}
	d = gcd(p_, q_), p_ /= d, q_ /= d;
	printf("%d/%d\n", p_, q_);
	return 0;
}

Compilation message

mag.c: In function 'append':
mag.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   13 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
mag.c: In function 'main':
mag.c:71:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
mag.c:75:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
mag.c:79:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# 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 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 419 ms 78532 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 510 ms 141480 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 420 ms 50788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 420 ms 51940 KB Output is correct
2 Correct 59 ms 5572 KB Output is correct
3 Incorrect 538 ms 144976 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5636 KB Output is correct
2 Incorrect 490 ms 51520 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 412 ms 47984 KB Output is correct
2 Incorrect 430 ms 50200 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 424 ms 51652 KB Output is correct
2 Correct 351 ms 27076 KB Output is correct