Submission #273057

# Submission time Handle Problem Language Result Execution time Memory
273057 2020-08-19T03:31:22 Z super_j6 Mag (COCI16_mag) C++14
120 / 120
508 ms 143992 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, ll>
#define f first
#define s second

const int mxn = 1000000;
int n;
int a[mxn], f1[mxn], f2[mxn];
vector<int> g[mxn];

void upd(int x, int v){
	f2[x] = max(f2[x], v);
	if(f2[x] > f1[x]) swap(f1[x], f2[x]);
}

void dfs(int c, int p){
	for(int i : g[c]){
		if(i == p) continue;
		dfs(i, c);
		if(!~-a[i]) upd(c, f1[i] + 1);
	}
}

void dfs2(int c, int p){
	for(int i : g[c]){
		if(i == p) continue;
		if(!~-a[c]) upd(i, (f1[i] + 1 == f1[c] ? f2[c] : f1[c]) + 1);
		dfs2(i, c);
	} 
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	
	for(int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i = 0; i < n; i++) cin >> a[i];
	
	dfs(0, -1);
	dfs2(0, -1);
	
	ll x = 1, y = 0;
	for(int i = 0; i < n; i++){
		ll w = a[i], z = f1[i] + f2[i] + 1;
		if(w * y < x * z) x = w, y = z;
	}
	ll g = __gcd(x, y);
	x /= g, y /= g;
	
	cout << x << "/" << y << endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 17 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 22 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 372 ms 83672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 508 ms 143992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 477 ms 126840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 430 ms 66936 KB Output is correct
2 Correct 351 ms 56184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 400 ms 67788 KB Output is correct
2 Correct 92 ms 29048 KB Output is correct
3 Correct 487 ms 129600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 28536 KB Output is correct
2 Correct 424 ms 67492 KB Output is correct
3 Correct 346 ms 46712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 372 ms 64364 KB Output is correct
2 Correct 419 ms 66040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 421 ms 67632 KB Output is correct
2 Correct 339 ms 46712 KB Output is correct