Submission #877165

#TimeUsernameProblemLanguageResultExecution timeMemory
877165noiaintMag (COCI16_mag)C++17
120 / 120
365 ms174152 KiB
#include <bits/stdc++.h> #define int long long using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif #define file "" #define mp make_pair #define fi first #define se second #define all(x) x.begin(), x.end() #define bit(x) (1LL << (x)) #define getbit(x, i) (((x) >> (i)) & 1) #define popcount __builtin_popcountll mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { return l + rd() % (r - l + 1); } const int N = 1e6 + 5; const int mod = (int)1e9 + 7; // 998244353; const int lg = 25; // lg + 1 const int oo = 1e9; const long long ooo = 1e18; template<class X, class Y> bool mini(X &a, Y b) { return a > b ? (a = b, true) : false; } template<class X, class Y> bool maxi(X &a, Y b) { return a < b ? (a = b, true) : false; } void add(int &a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } int n; int a[N]; vector<int> adj[N]; int f[N], f2[N]; void update(int u, int c) { if (f[u] < c) f2[u] = f[u], f[u] = c; else maxi(f2[u], c); } void dfsF(int u, int p) { for (int v : adj[u]) if (v != p) { dfsF(v, u); if (a[v] == 1) update(u, f[v] + 1); } } void dfsG(int u, int p) { for (int v : adj[u]) if (v != p) { if (a[u] == 1) { if (f[u] == f[v] + 1) update(v, f2[u] + 1); else update(v, f[u] + 1); } dfsG(v, u); } } void process() { cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; ++i) cin >> a[i]; dfsF(1, 0); dfsG(1, 0); pair<int, int> ans = mp(1e18, 1); for (int i = 1; i <= n; ++i) { if (a[i] * ans.se < ans.fi * (f[i] + f2[i] + 1)) ans = mp(a[i], f[i] + f2[i] + 1); } int x = __gcd(ans.fi, ans.se); ans.fi /= x; ans.se /= x; cout << ans.fi << '/' << ans.se; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); int tc = 1; // cin >> tc; while (tc--) { process(); } 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...