#include "race.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const ll maxn = 2e5 + 2;
ll f[(ll)1e6 + 2];
ll sz[maxn],h[maxn],k;
vector<pii> a[maxn];
ll ans = LLONG_MAX;
map<ll,ll> mp[maxn];
void dfs(ll u, ll w, ll pa) {
for (pii tmp : a[u]) {
ll wv,v; tie(v,wv) = tmp;
if (v==pa) continue;
h[v] = h[u] + 1;
dfs(v,w+wv,u);
ll t = mp[v][w+wv];
mp[v][w+wv] = (t == 0) ? h[v] : min(t,h[v]);
if (mp[u].size() < mp[v].size()) swap(mp[u],mp[v]);
for (pii val : mp[v]) {
if (val.second==0) continue;
if (val.first - w < k && mp[u][k-val.first+w] != 0) {
ans = min(ans, mp[u][k-val.first+w] - h[u] + val.second - h[u]);
}
}
mp[u].insert(mp[v].begin(),mp[v].end());
if (mp[u][w + k] != 0) {
ans = min(ans, mp[u][w+k]-h[u]);
}
}
}
int best_path(int _n, int _k, int _h[][2], int _L[])
{
k = _k;
for (ll i = 0; i < _n - 1; i++){
ll x = _h[i][0], y= _h[i][1];
ll w = _L[i];
a[x].push_back({y,w});
a[y].push_back({x,w});
}
h[0] = 1;
dfs(0,0,-1);
return (int) ans == LLONG_MAX ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
6 ms |
16888 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
6 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16720 KB |
Output is correct |
6 |
Correct |
7 ms |
16720 KB |
Output is correct |
7 |
Correct |
6 ms |
16936 KB |
Output is correct |
8 |
Correct |
5 ms |
16720 KB |
Output is correct |
9 |
Correct |
5 ms |
16860 KB |
Output is correct |
10 |
Correct |
4 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16736 KB |
Output is correct |
12 |
Correct |
5 ms |
16860 KB |
Output is correct |
13 |
Correct |
8 ms |
16720 KB |
Output is correct |
14 |
Correct |
7 ms |
16720 KB |
Output is correct |
15 |
Correct |
7 ms |
16720 KB |
Output is correct |
16 |
Correct |
7 ms |
16720 KB |
Output is correct |
17 |
Correct |
8 ms |
15184 KB |
Output is correct |
18 |
Correct |
8 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
6 ms |
16888 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
6 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16720 KB |
Output is correct |
6 |
Correct |
7 ms |
16720 KB |
Output is correct |
7 |
Correct |
6 ms |
16936 KB |
Output is correct |
8 |
Correct |
5 ms |
16720 KB |
Output is correct |
9 |
Correct |
5 ms |
16860 KB |
Output is correct |
10 |
Correct |
4 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16736 KB |
Output is correct |
12 |
Correct |
5 ms |
16860 KB |
Output is correct |
13 |
Correct |
8 ms |
16720 KB |
Output is correct |
14 |
Correct |
7 ms |
16720 KB |
Output is correct |
15 |
Correct |
7 ms |
16720 KB |
Output is correct |
16 |
Correct |
7 ms |
16720 KB |
Output is correct |
17 |
Correct |
8 ms |
15184 KB |
Output is correct |
18 |
Correct |
8 ms |
15184 KB |
Output is correct |
19 |
Incorrect |
4 ms |
16724 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
6 ms |
16888 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
6 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16720 KB |
Output is correct |
6 |
Correct |
7 ms |
16720 KB |
Output is correct |
7 |
Correct |
6 ms |
16936 KB |
Output is correct |
8 |
Correct |
5 ms |
16720 KB |
Output is correct |
9 |
Correct |
5 ms |
16860 KB |
Output is correct |
10 |
Correct |
4 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16736 KB |
Output is correct |
12 |
Correct |
5 ms |
16860 KB |
Output is correct |
13 |
Correct |
8 ms |
16720 KB |
Output is correct |
14 |
Correct |
7 ms |
16720 KB |
Output is correct |
15 |
Correct |
7 ms |
16720 KB |
Output is correct |
16 |
Correct |
7 ms |
16720 KB |
Output is correct |
17 |
Correct |
8 ms |
15184 KB |
Output is correct |
18 |
Correct |
8 ms |
15184 KB |
Output is correct |
19 |
Incorrect |
131 ms |
47176 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
6 ms |
16888 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
6 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16720 KB |
Output is correct |
6 |
Correct |
7 ms |
16720 KB |
Output is correct |
7 |
Correct |
6 ms |
16936 KB |
Output is correct |
8 |
Correct |
5 ms |
16720 KB |
Output is correct |
9 |
Correct |
5 ms |
16860 KB |
Output is correct |
10 |
Correct |
4 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16736 KB |
Output is correct |
12 |
Correct |
5 ms |
16860 KB |
Output is correct |
13 |
Correct |
8 ms |
16720 KB |
Output is correct |
14 |
Correct |
7 ms |
16720 KB |
Output is correct |
15 |
Correct |
7 ms |
16720 KB |
Output is correct |
16 |
Correct |
7 ms |
16720 KB |
Output is correct |
17 |
Correct |
8 ms |
15184 KB |
Output is correct |
18 |
Correct |
8 ms |
15184 KB |
Output is correct |
19 |
Incorrect |
4 ms |
16724 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |