#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));
}
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) >> 1;
if (dfs2(1, 1, mid) > dfs2(1, 1, mid + 1)) {
l = mid + 1;
} else {
r = mid;
}
}
cout<<dfs2(1, 1, l);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
18 ms |
14456 KB |
Output is correct |
4 |
Correct |
18 ms |
14456 KB |
Output is correct |
5 |
Correct |
18 ms |
14456 KB |
Output is correct |
6 |
Correct |
18 ms |
14476 KB |
Output is correct |
7 |
Correct |
19 ms |
14456 KB |
Output is correct |
8 |
Correct |
18 ms |
14456 KB |
Output is correct |
9 |
Correct |
18 ms |
14456 KB |
Output is correct |
10 |
Correct |
19 ms |
14452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
18 ms |
14456 KB |
Output is correct |
4 |
Correct |
18 ms |
14456 KB |
Output is correct |
5 |
Correct |
18 ms |
14456 KB |
Output is correct |
6 |
Correct |
18 ms |
14476 KB |
Output is correct |
7 |
Correct |
19 ms |
14456 KB |
Output is correct |
8 |
Correct |
18 ms |
14456 KB |
Output is correct |
9 |
Correct |
18 ms |
14456 KB |
Output is correct |
10 |
Correct |
19 ms |
14452 KB |
Output is correct |
11 |
Incorrect |
14 ms |
14456 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
18 ms |
14456 KB |
Output is correct |
4 |
Correct |
18 ms |
14456 KB |
Output is correct |
5 |
Correct |
18 ms |
14456 KB |
Output is correct |
6 |
Correct |
18 ms |
14476 KB |
Output is correct |
7 |
Correct |
19 ms |
14456 KB |
Output is correct |
8 |
Correct |
18 ms |
14456 KB |
Output is correct |
9 |
Correct |
18 ms |
14456 KB |
Output is correct |
10 |
Correct |
19 ms |
14452 KB |
Output is correct |
11 |
Incorrect |
14 ms |
14456 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |