Submission #137599

# Submission time Handle Problem Language Result Execution time Memory
137599 2019-07-28T07:16:24 Z abacaba Mag (COCI16_mag) C++14
0 / 120
2144 ms 133100 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int inf = 2e18;
const int N = 1e6 + 15;
int n, m, a[N], sz[N];
pair <int, int> dp[N];
vector <int> g[N];
pair <int, int> ans = make_pair(0LL, 0LL);
bool used[N];

void dfs(int v, int p = -1) {
    sz[v] = 1;
    for(int to : g[v])
        if(!used[to] && to != p) {
            dfs(to, v);
            sz[v] += sz[to];
        }
}

int centroid(int v, int p, int n) {
    for(int to : g[v])
        if(!used[to] && to != p && sz[to] > n / 2)
            return centroid(to, v, n);
    return v;
}

inline bool ls(pair <int, int> x, pair <int, int> y) {
    return x.first * y.second < x.second * y.first;
}

void calc(int v, int p) {
    dp[v] = make_pair(a[v], 1LL);
    for(int to : g[v]) {
        if(!used[to] && to != p) {
            calc(to, v);
            pair <int, int> x = make_pair(a[v] * dp[to].first, 1LL + dp[to].second);
            if(ls(x, dp[v])) {
                dp[v] = x;
                int c = __gcd(dp[v].first, dp[v].second);
                if(c)
                    dp[v].first /= c, dp[v].second /= c;
            }
        }
    }
}

void solve(int v) {
    used[v] = true;
    dfs(v);
    multiset <pair <int, int> > is;
    for(int to : g[v])
        if(!used[to]) {
            calc(to, v);
            is.insert(dp[to]);
        }
    for(int to : g[v]) {
        if(!used[to]) {
            pair <int, int> x = dp[to];
            is.erase(is.find(x));
            if(!is.empty()) {
                x.first *= is.begin()->first, x.second += is.begin()->second;
                if(ans == make_pair(0LL, 0LL) || ls(x, ans)) {
                    ans = x;
                    int c = __gcd(ans.first, ans.second);
                    if(c)
                        ans.first /= c, ans.second /= c;
                }
            }
            is.insert(dp[to]);
        }
    }
    for(int to : g[v])
        if(!used[to])
            solve(centroid(to, v, sz[to]));
}

#undef int

int main() {
    #define int long long
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        pair <int, int> x = {a[i], 1LL};
        if(ans == make_pair(0LL, 0LL) || a[i] < ans.first)
            ans = x;
    }
    dfs(1, -1);
    solve(centroid(1, -1, n));
    cout << ans.first << '/' << ans.second << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Incorrect 25 ms 24056 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 23928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1706 ms 109792 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 23928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2144 ms 133100 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1241 ms 91212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 896 ms 111492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 249 ms 32340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1073 ms 93528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1948 ms 91972 KB Output isn't correct
2 Halted 0 ms 0 KB -