Submission #490970

#TimeUsernameProblemLanguageResultExecution timeMemory
490970DiriiMag (COCI16_mag)C++14
120 / 120
503 ms256452 KiB
// #pragma GCC target("avx2") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define ll int #define cll const ll #define lp(a, b, c) for(ll a = b; a <= c; ++a) #define lpd(a, b, c) for(ll a = b; a >= c; --a) #define vec(a) vector<a> #define pp(a, b) pair<a, b> #define EACHCASE lpd(cs, read(), 1) #define Fname "f" using namespace std; template <typename T> inline void Read(T &x){ x = 0; char c; ll dau = 1; while(!isdigit(c = getchar())) if(c == '-') dau = -1; do{ x = x * 10 + c - '0'; } while (isdigit(c = getchar())); x *= dau; } ll read(){ ll tmp; cin >> tmp; return tmp; } void giuncute(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void OF(){ if(fopen(Fname".inp", "r")){ freopen(Fname".inp", "r", stdin); freopen(Fname".out", "w", stdout); } else if(fopen(Fname".in", "r")){ freopen(Fname".in", "r", stdin); freopen(Fname".out", "w", stdout); } } cll mxn = 1e6 + 7; ll n, a[mxn], up[mxn] = {0}, down[mxn] = {0}, anstu = 1e9, ansmau = 1, cnt[mxn] = {0}; vec(ll) g[mxn]; void init(ll u, ll p){ for(ll v : g[u]){ if(v == p) continue; init(v, u); down[u] = max(down[u], down[v]); } if(a[u] == 1) ++down[u]; else down[u] = 0; } void dfs(ll u, ll p){ vec(pp(ll, ll)) dp; for(ll v : g[u]){ if(v == p) dp.push_back({up[u], v}); else dp.push_back({down[v], v}); } sort(dp.begin(), dp.end()); for(ll v : g[u]){ if(v == p) continue; if(dp.back().second != v) up[v] = dp.back().first; else if(dp.size() > 1) up[v] = dp[dp.size() - 2].first; if(a[u] != 1) up[v] = 0; else ++up[v]; dfs(v, u); } if(dp.empty()) anstu = min(anstu, a[u]); else if(dp.size() == 1){ long long tu = a[u], mau = dp[0].first + 1; if(tu * ansmau < mau * anstu) anstu = tu, ansmau = mau; } else{ long long tu = a[u], mau = dp.back().first + dp[dp.size() - 2].first + 1; if(tu * ansmau < mau * anstu) anstu = tu, ansmau = mau; } } int main(){ giuncute(); #ifndef ONLINE_JUDGE OF(); #endif cin >> n; lp(i, 1, n - 1){ ll u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } lp(i, 1, n) cin >> a[i]; init(1, -1); dfs(1, -1); ll tmp = __gcd(anstu, ansmau); cout << anstu / tmp << '/' << ansmau / tmp; }

Compilation message (stderr)

mag.cpp: In function 'void OF()':
mag.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(Fname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(Fname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...