Submission #930140

#TimeUsernameProblemLanguageResultExecution timeMemory
930140Yang8onMag (COCI16_mag)C++14
72 / 120
374 ms148140 KiB
#include <bits/stdc++.h> #define gsn "Mag" #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define ll long long #define maxn 1000005 #define gb(i, j) ((i >> j) & 1) #define pii pair<int, int> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll GetRandom(ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rng); } int n; vector<int> o[maxn]; int a[maxn], dp[maxn][2], f[maxn]; void dfs(int u, int par) { for(int v : o[u]) { if(v == par) continue; dfs(v, u); if(a[v] == 1) { if(dp[v][0] + a[v] > dp[u][0]) { dp[u][1] = dp[u][0]; dp[u][0] = dp[v][0] + a[v]; } else if(dp[v][0] + a[v] > dp[u][1]) { dp[u][1] = dp[v][0] + a[v]; } } } } void dfs2(int u, int par) { for(int v : o[u]) { if(v == par) continue; if(a[u] == 1) { if(f[u] + a[u] > f[v]) f[v] = f[u] + a[u]; if(dp[v][0] == dp[v][1]) f[v] = max(f[v], dp[v][0] + 1); else if(dp[v][0] + 1 == dp[u][0]) f[v] = max(f[v], dp[u][1] + a[u]); else f[v] = max(f[v], dp[u][0] + 1); } dfs2(v, u); } } int main() { // freopen(gsn".inp","r",stdin); // freopen(gsn".out","w",stdout); ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; fi(i, 1, n - 1) { int x, y; cin >> x >> y; o[x].push_back(y); o[y].push_back(x); } fi(i, 1, n) cin >> a[i]; dfs(1, 0); dfs2(1, 0); ll P = -1, Q = -1; fi(i, 1, n) { ll tu = a[i]; ll mau = max({ dp[i][0] + dp[i][1], dp[i][0] + f[i], dp[i][1] + f[i] }) + 1; ll Gcd = __gcd(tu, mau); tu /= Gcd; mau /= Gcd; if(P == -1) P = tu, Q = mau; else if(P * mau > Q * tu) P = tu, Q = mau; } cout << P << "/" << Q ; /// ------------------check time!-----------------/// cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...