Submission #245776

# Submission time Handle Problem Language Result Execution time Memory
245776 2020-07-07T11:33:42 Z Vladikus004 Mag (COCI16_mag) C++14
24 / 120
499 ms 163448 KB
#include <bits/stdc++.h>
#define int long long
#define inf 2e9
#define mp make_pair
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 1000000 + 3;
int n, z[N], p[N];
pair <ld, pii> dp[N], ans;
vector <vector <int> > v;

void solve1(int x, int pr){
    p[x] = 1;
    for (auto u: v[x]){
        if (p[u]) continue;
        if (pr != -1)
            dp[x] = min(
            mp(ld(dp[pr].second.first * z[x]) / (dp[pr].second.second + 1), mp(dp[pr].second.first * z[x], dp[pr].second.second + 1)),
            mp(ld(z[x]), mp(z[x], 1LL))
            );
        else
            dp[x] = {ld(z[x]), {z[x], 1}};
        solve1(u, x);
    }
    ans = min(ans, dp[x]);
}

pii ndp[N];

void solve2(int x, int pr){
    p[x] = 1;
    for (auto u: v[x]){
        if (p[u]) continue;
        if (pr == -1){
            ndp[x] = {z[x], 1};
        }else{
            if (z[x] == 1){
                if (ndp[pr].first > ndp[pr].second) ndp[x] = {1, 1};
                else{
                    ndp[x].first = ndp[pr].first * z[x];
                    ndp[x].second = ndp[pr].second + 1;
                }
            }else{
                ndp[x].first = ndp[pr].first * z[x];
                ndp[x].second = ndp[pr].second + 1;
            }
        }
        int gc = __gcd(ndp[x].first, ndp[x].second);
        ndp[x].first /= gc;
        ndp[x].second /= gc;
    ans = min(ans, {ld(ndp[x].first) / ld(ndp[x].second), {ndp[x].first, ndp[x].second}});
        solve2(u, x);

    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n;
    v.resize(n);
    int st = -1;
    for (int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        --a; --b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for (int i = 0; i < n; i++) if (v[i].size() == 1) st = i;
    for (int i = 0; i < n; i++) cin >> z[i];
    ans = {inf * inf, {inf, inf}};
    if (n <= 1000){
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++) dp[j] = {inf * inf, {inf, inf}}, p[j] = 0;
            solve1(i, -1);
        }
    }else{
        solve2(st, -1);
    }
    cout << ans.second.first << "/" << ans.second.second << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 512 KB Output is correct
2 Correct 11 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 512 KB Output is correct
2 Correct 31 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 409 ms 131888 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Incorrect 499 ms 163448 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 472 ms 160676 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 441 ms 86008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 422 ms 88288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 89 ms 9612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 409 ms 81236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 484 ms 87084 KB Output isn't correct
2 Halted 0 ms 0 KB -