// #pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define sz(x) (int)x.size()
#define bit(a, i) ((a>>i)&1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int K = 800;
const ll inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 100;
int n, m;
ll ans, OK;
vector<int>e[maxn];
vector<pii>g[maxn];
void dfs(int v, int pr, int timer){
bool ok = 0;
for(pii to: g[v]){
if(to.f != pr){
ok = 1;
dfs(to.f, v, timer+to.s);
}
}
if(!ok) e[v].pb(timer);
else{
for(pii to: g[v]){
for(auto to1: e[to.f]) e[v].pb(to1);
}
}
sort(e[v].begin(), e[v].end());
}
void count(int x, int y, int v, int pr){
bool ok = 0;
for(pii to: g[v]){
int u = to.f;
if(u == pr) continue;
ok = 1;
if(e[u][0]+y >= x){
int nwy = y - min(e[u][0]+y-x, to.s);
ans += min(e[u][0]+y-x, to.s);
count(x, nwy, u, v);
continue;
}
if(e[u].back()+y <= x){
int nwy = x-e[u].back();
ans += x-(e[u].back()+y);
count(x, nwy, u, v);
continue;
}
count(x, y, u, v);
}
if(!ok){
for(auto to: e[v]){
if(to+y != x) OK = 0;
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
vector<int>a;
for(int i=2; i<=n+m; i++){
int v, w; cin >> v >> w;
g[v].pb({i, w});
}
dfs(1, 0, 0);
ll sum = inf;
for(int i=0; i<=500; i++){
OK = 1;
ans = 0;
count(i, 0, 1, 0);
if(OK) {
sum = min(sum, ans);
}
}
cout << sum;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
47180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
24 ms |
47188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
47180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
47180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |