#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
const long long inf = 1e18;
int n, m;
vector< pair<int,int> > G[N];
long long dp[N], tmp[N], dist[N];
int tin[N], tout[N], TIME;
// leaf: n + 1 -> n + m
bool inside(int fixed, int u) {
return (tin[fixed] >= tin[u] && tin[fixed] <= tout[u]);
}
void dfs(int u) {
tin[u] = ++TIME;
for (auto e : G[u]) {
int v = e.second, c = e.first;
dist[v] = dist[u] + c;
dfs(v);
}
tout[u] = TIME;
}
void solve(int u) {
for (auto e : G[u]) {
int v = e.second, c = e.first;
solve(v);
}
for (int fixed = n + 1; fixed <= n + m; ++fixed) tmp[fixed] = 0;
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
if (!inside(fixed, u)) continue;
for (auto e : G[u]) {
int v = e.second;
long long mn = inf;
for (int leaf = n + 1; leaf <= n + m; ++leaf) {
if (!inside(leaf, v)) continue;
long long cur = dp[leaf] + abs(dist[leaf] - dist[fixed]);
mn = min(mn, cur);
}
tmp[fixed] += mn;
}
}
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
if (inside(fixed, u)) dp[fixed] = tmp[fixed];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
int nNode = n + m;
for (int i = 2; i <= nNode; ++i) {
int p, c; cin >> p >> c;
G[p].push_back(make_pair(c, i));
}
dfs(1);
solve(1);
long long ans = inf;
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
ans = min(ans, dp[fixed]);
}
cout << ans << endl;
}
Compilation message
fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:30:21: warning: unused variable 'c' [-Wunused-variable]
int v = e.second, c = e.first;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18580 KB |
Output is correct |
2 |
Correct |
0 ms |
18580 KB |
Output is correct |
3 |
Correct |
3 ms |
18580 KB |
Output is correct |
4 |
Correct |
3 ms |
18580 KB |
Output is correct |
5 |
Correct |
6 ms |
18580 KB |
Output is correct |
6 |
Correct |
3 ms |
18580 KB |
Output is correct |
7 |
Correct |
6 ms |
18580 KB |
Output is correct |
8 |
Correct |
0 ms |
18580 KB |
Output is correct |
9 |
Correct |
0 ms |
18580 KB |
Output is correct |
10 |
Correct |
0 ms |
18580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
18580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18580 KB |
Output is correct |
2 |
Correct |
0 ms |
18580 KB |
Output is correct |
3 |
Correct |
3 ms |
18580 KB |
Output is correct |
4 |
Correct |
3 ms |
18580 KB |
Output is correct |
5 |
Correct |
6 ms |
18580 KB |
Output is correct |
6 |
Correct |
3 ms |
18580 KB |
Output is correct |
7 |
Correct |
6 ms |
18580 KB |
Output is correct |
8 |
Correct |
0 ms |
18580 KB |
Output is correct |
9 |
Correct |
0 ms |
18580 KB |
Output is correct |
10 |
Correct |
0 ms |
18580 KB |
Output is correct |
11 |
Incorrect |
0 ms |
18580 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18580 KB |
Output is correct |
2 |
Correct |
0 ms |
18580 KB |
Output is correct |
3 |
Correct |
3 ms |
18580 KB |
Output is correct |
4 |
Correct |
3 ms |
18580 KB |
Output is correct |
5 |
Correct |
6 ms |
18580 KB |
Output is correct |
6 |
Correct |
3 ms |
18580 KB |
Output is correct |
7 |
Correct |
6 ms |
18580 KB |
Output is correct |
8 |
Correct |
0 ms |
18580 KB |
Output is correct |
9 |
Correct |
0 ms |
18580 KB |
Output is correct |
10 |
Correct |
0 ms |
18580 KB |
Output is correct |
11 |
Incorrect |
0 ms |
18580 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |