Submission #310602

# Submission time Handle Problem Language Result Execution time Memory
310602 2020-10-07T13:41:22 Z shivensinha4 Mag (COCI16_mag) C++17
108 / 120
780 ms 262144 KB
#include <bits/stdc++.h> 
using namespace std; 
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'

const int MXN = 1e6;
const ll INF = 1e15;
vi adj[MXN+1];
int val[MXN+1];
int chain[MXN+1];
int n;
pair<ll, ll> ans = {INF, 1};

void update(ll nu, ll de) {
	ll g = __gcd(nu, de);
	pair<ll, ll> temp = {nu/g, de/g};
	if (ans.first == INF) ans = temp;
	else {
		pair<ll, ll> pre = ans, pree = temp;
		ll lcm = temp.second * (pre.second / __gcd(temp.second, pre.second));
		temp.first *= lcm / temp.second, pre.first *= lcm / pre.second;
		if (temp.first < pre.first) ans = pree;
	}
}


void dfs(int p, int parent) {
	for (int i: adj[p]) if (i != parent) {
		dfs(i, p);
		chain[p] = max(chain[p], chain[i]);
	}
	
	if (val[p] == 1) chain[p] += 1;
	else chain[p] = 0;
}


void dfs2(int p, int parent, int pval) {
	vi all, mx(2);
	all.push_back(pval);
	for (int i: adj[p]) if (i != parent) all.push_back(chain[i]);
	sort(all.begin(), all.end(), greater<int>());
	for_(i, 0, min((int) all.size(), 2)) mx[i] = all[i];
	
	update(val[p], mx[0]+mx[1]+1);
	
	for (auto i: adj[p]) if (i != parent) dfs2(i, p, val[p] == 1 ? pval+1 : 0);
}


int main() {
	#ifdef shiven
	freopen("test.in", "r", stdin);
	#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n;
	for_(i, 0, n-1) {
		int a, b; cin >> a >> b;
		a -= 1; b -= 1;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	for_(i, 0, n) cin >> val[i];
	
	dfs(0, 0);
	dfs2(0, 0, 0);
	cout << ans.first << "/" << ans.second << 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 16 ms 23936 KB Output is correct
2 Correct 17 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 592 ms 144248 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 740 ms 262144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 763 ms 262144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 580 ms 78200 KB Output is correct
2 Correct 432 ms 64120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 548 ms 84324 KB Output is correct
2 Correct 112 ms 29480 KB Output is correct
3 Correct 780 ms 262144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 29176 KB Output is correct
2 Correct 593 ms 79736 KB Output is correct
3 Correct 487 ms 51576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 546 ms 78836 KB Output is correct
2 Correct 575 ms 79480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 606 ms 79480 KB Output is correct
2 Correct 513 ms 51704 KB Output is correct