제출 #137599

#제출 시각아이디문제언어결과실행 시간메모리
137599abacabaMag (COCI16_mag)C++14
0 / 120
2144 ms133100 KiB
#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 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...