Submission #127329

# Submission time Handle Problem Language Result Execution time Memory
127329 2019-07-09T08:36:57 Z srvlt Fireworks (APIO16_fireworks) C++14
7 / 100
932 ms 14476 KB
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define int long long
using namespace std;

const int N = 3e5 + 3, inf = 1e18;
int n, m;
vector <pair <int, int> > q[N];
vector <int> leaves[N];

void dfs1(int v, int p) {
    for (auto i : q[v]) {
        int to = i.fi, w = i.se;
        if (to != p) {
            dfs1(to, v);
            for (auto j : leaves[to]) {
                leaves[v].pb(j + w);
            }
        }
    }
    if (q[v].size() == 1 && v != 1) {
        leaves[v] = {0};
    }
}

int dfs2(int v, int p, int k) {
    if (q[v].size() == 1 && v != 1) {
        return abs(k);
    }
    int ans = inf;
    for (auto i : leaves[v]) {
        int delta = k - i, res = 0;
        for (auto j : q[v]) {
            int to = j.fi, w = j.se;
            if (to != p) {
                res += dfs2(to, v, k - w - delta);
            }
        }
        ans = min(ans, res + abs(delta));
    }
    int res2 = 0;
    for (auto j : q[v]) {
        int to = j.fi, w = j.se;
        if (to != p) {
            res2 += dfs2(to, v, k - w);
        }
    }
    ans = min(ans, res2);
    return ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m;
    for (int i = 2; i <= n + m; i++) {
        int x, y;
        cin>>x>>y;
        q[x].pb({i, y});
        q[i].pb({x, y});
    }
    dfs1(1, 1);
    int l = 0, r = 1e9, magic = 31;
    while (magic--) {
        int mid = (l + r) >> 1LL;
        if (dfs2(1, 1, mid) > dfs2(1, 1, mid + 1)) {
            l = mid + 1;
        }   else {
            r = mid;
        }
    }
    cout<<dfs2(1, 1, l);
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 18 ms 14472 KB Output is correct
4 Correct 17 ms 14456 KB Output is correct
5 Correct 17 ms 14456 KB Output is correct
6 Correct 18 ms 14456 KB Output is correct
7 Correct 18 ms 14456 KB Output is correct
8 Correct 18 ms 14456 KB Output is correct
9 Correct 17 ms 14456 KB Output is correct
10 Correct 18 ms 14460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14468 KB Output is correct
2 Correct 36 ms 14456 KB Output is correct
3 Incorrect 932 ms 14476 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 18 ms 14472 KB Output is correct
4 Correct 17 ms 14456 KB Output is correct
5 Correct 17 ms 14456 KB Output is correct
6 Correct 18 ms 14456 KB Output is correct
7 Correct 18 ms 14456 KB Output is correct
8 Correct 18 ms 14456 KB Output is correct
9 Correct 17 ms 14456 KB Output is correct
10 Correct 18 ms 14460 KB Output is correct
11 Correct 15 ms 14468 KB Output is correct
12 Correct 36 ms 14456 KB Output is correct
13 Incorrect 932 ms 14476 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 18 ms 14472 KB Output is correct
4 Correct 17 ms 14456 KB Output is correct
5 Correct 17 ms 14456 KB Output is correct
6 Correct 18 ms 14456 KB Output is correct
7 Correct 18 ms 14456 KB Output is correct
8 Correct 18 ms 14456 KB Output is correct
9 Correct 17 ms 14456 KB Output is correct
10 Correct 18 ms 14460 KB Output is correct
11 Correct 15 ms 14468 KB Output is correct
12 Correct 36 ms 14456 KB Output is correct
13 Incorrect 932 ms 14476 KB Output isn't correct
14 Halted 0 ms 0 KB -