Submission #490970

# Submission time Handle Problem Language Result Execution time Memory
490970 2021-11-30T02:32:03 Z Dirii Mag (COCI16_mag) C++14
120 / 120
503 ms 256452 KB
// #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

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 time Memory Grader output
1 Correct 13 ms 23972 KB Output is correct
2 Correct 13 ms 23816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23812 KB Output is correct
2 Correct 13 ms 23864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 140920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23720 KB Output is correct
2 Correct 503 ms 252496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 470 ms 237732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 367 ms 81220 KB Output is correct
2 Correct 267 ms 66308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 71240 KB Output is correct
2 Correct 65 ms 29684 KB Output is correct
3 Correct 482 ms 256452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 29544 KB Output is correct
2 Correct 379 ms 78796 KB Output is correct
3 Correct 310 ms 53464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 377 ms 81396 KB Output is correct
2 Correct 355 ms 74896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 379 ms 82556 KB Output is correct
2 Correct 301 ms 53580 KB Output is correct