Submission #209062

# Submission time Handle Problem Language Result Execution time Memory
209062 2020-03-13T04:32:18 Z Fischer Mag (COCI16_mag) C++14
120 / 120
705 ms 229056 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
vector<int> g[maxn], len[maxn];
int a[maxn];
int memo[maxn];
int n;

void add(int x, int t) {
	if (len[x][0] <= t) swap(len[x][0], t);
	len[x][1] = max(len[x][1], t);
}

int get(int x, int t=0) {
	return (a[x] == 1) * (len[x][t] + 1);
}

int dfs(int x, int p = 0) {
	len[x] = {0, 0};
	for (int v : g[x]) {
		if (v == p) continue;
		add(x, dfs(v, x));
	}
	return get(x);
}

int dfs_root(int x, int p = 0) {
	if (p) {
		int l = (a[p] == 1) *  get(p, len[p][0] == get(x));
		add(x, l);
	}
	memo[x] = len[x][0] + len[x][1];
	for (int v : g[x]) {
		if (v == p) continue;
		dfs_root(v, x);
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n-1; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%d", a+i);
	}
	dfs(1);
	dfs_root(1);
	int x = -1, y = -1;
	for (int i = 1; i <= n; ++i) {
		if (x == -1 or x *1ll* (memo[i] + 1) > a[i] *1ll* y) {
			x = a[i];
			y = memo[i] + 1;
		}
	}
	int g = __gcd(x, y);
	printf("%d/%d\n", x/g, y/g);
	return 0;
}

Compilation message

mag.cpp: In function 'int dfs_root(int, int)':
mag.cpp:37:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
mag.cpp: In function 'int main()':
mag.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
mag.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
mag.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 47352 KB Output is correct
2 Correct 33 ms 47352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 47352 KB Output is correct
2 Correct 34 ms 47352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 543 ms 155148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47224 KB Output is correct
2 Correct 689 ms 225536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 672 ms 224676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 132604 KB Output is correct
2 Correct 437 ms 110056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 586 ms 135556 KB Output is correct
2 Correct 136 ms 55928 KB Output is correct
3 Correct 705 ms 229056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 55800 KB Output is correct
2 Correct 591 ms 133452 KB Output is correct
3 Correct 566 ms 90744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 537 ms 128368 KB Output is correct
2 Correct 575 ms 133368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 597 ms 133688 KB Output is correct
2 Correct 539 ms 90744 KB Output is correct