#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <bitset>
#include <unordered_map>
#include "race.h"
using namespace std;
const int n = 200003;
int s, k;
vector<pair<int, int>> g[n];
long long h[n];
int d[n];
bool mark[n];
int ans = -1;
void answer(int x)
{
if (ans == -1)
ans = x;
else
ans = min(ans, x);
}
void calc_height(int v, int p)
{
d[v] = 1;
for (pair<int, int> to : g[v])
{
if (to.first == p || mark[to.first])
continue;
h[to.first] = h[v] + to.second;
calc_height(to.first, v);
d[v] += d[to.first];
}
}
int center(int v, int p, int sz)
{
int t = -1;
for (pair<int, int> to : g[v])
{
if (mark[to.first] || to.first == p)
continue;
if (d[to.first] > sz / 2)
{
t = to.first;
break;
}
}
return t == -1 ? v : center(t, v, sz);
}
void get_all(int v, int p, vector<pair<long long, int>>& st, long long dp, int l)
{
st.push_back({ dp, l });
for (auto to : g[v])
{
if (to.first == p || mark[to.first])
continue;
get_all(to.first, v, st, dp + to.second, l + 1);
}
}
void solve(int v)
{
//cout << v;
calc_height(v, -1);
v = center(v, -1, d[v]);
mark[v] = true;
calc_height(v, -1);
map<long long, int> st;
for (auto to : g[v])
{
if (mark[to.first])
continue;
vector<pair<long long, int>> dst;
get_all(to.first, v, dst, to.second, 1);
for (auto x : dst)
{
if (x.first > k)
continue;
if (x.first == k)
answer(x.second);
else if (st.count(k - x.first))
answer(x.second + st[k - x.first]);
}
for (auto x : dst)
{
st[x.first] = st[x.first] != 0 ? min(x.second, st[x.first]) : x.second;
}
solve(to.first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
s = N;
k = K;
for (int i = 0; i < s - 1; i++)
{
int a, b, l;
a = H[i][0];
b = H[i][1];
l = L[i];
g[a].push_back({ b, l });
g[b].push_back({ a, l });
}
solve(0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4988 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
5 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4936 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4988 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
5 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4936 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5180 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5136 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
7 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4988 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
5 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4936 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
329 ms |
13460 KB |
Output is correct |
20 |
Correct |
318 ms |
13344 KB |
Output is correct |
21 |
Correct |
321 ms |
13512 KB |
Output is correct |
22 |
Correct |
271 ms |
13360 KB |
Output is correct |
23 |
Correct |
344 ms |
13408 KB |
Output is correct |
24 |
Correct |
171 ms |
12660 KB |
Output is correct |
25 |
Correct |
310 ms |
19388 KB |
Output is correct |
26 |
Correct |
157 ms |
20820 KB |
Output is correct |
27 |
Correct |
392 ms |
18728 KB |
Output is correct |
28 |
Correct |
1203 ms |
61660 KB |
Output is correct |
29 |
Correct |
1196 ms |
60420 KB |
Output is correct |
30 |
Correct |
394 ms |
18648 KB |
Output is correct |
31 |
Correct |
354 ms |
18564 KB |
Output is correct |
32 |
Correct |
467 ms |
18856 KB |
Output is correct |
33 |
Correct |
644 ms |
21336 KB |
Output is correct |
34 |
Correct |
960 ms |
37828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4988 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
5 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4936 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5180 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5136 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
7 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5068 KB |
Output is correct |
39 |
Correct |
329 ms |
13460 KB |
Output is correct |
40 |
Correct |
318 ms |
13344 KB |
Output is correct |
41 |
Correct |
321 ms |
13512 KB |
Output is correct |
42 |
Correct |
271 ms |
13360 KB |
Output is correct |
43 |
Correct |
344 ms |
13408 KB |
Output is correct |
44 |
Correct |
171 ms |
12660 KB |
Output is correct |
45 |
Correct |
310 ms |
19388 KB |
Output is correct |
46 |
Correct |
157 ms |
20820 KB |
Output is correct |
47 |
Correct |
392 ms |
18728 KB |
Output is correct |
48 |
Correct |
1203 ms |
61660 KB |
Output is correct |
49 |
Correct |
1196 ms |
60420 KB |
Output is correct |
50 |
Correct |
394 ms |
18648 KB |
Output is correct |
51 |
Correct |
354 ms |
18564 KB |
Output is correct |
52 |
Correct |
467 ms |
18856 KB |
Output is correct |
53 |
Correct |
644 ms |
21336 KB |
Output is correct |
54 |
Correct |
960 ms |
37828 KB |
Output is correct |
55 |
Correct |
27 ms |
6476 KB |
Output is correct |
56 |
Correct |
20 ms |
5988 KB |
Output is correct |
57 |
Correct |
189 ms |
13116 KB |
Output is correct |
58 |
Correct |
69 ms |
11860 KB |
Output is correct |
59 |
Correct |
401 ms |
30372 KB |
Output is correct |
60 |
Correct |
1232 ms |
58992 KB |
Output is correct |
61 |
Correct |
436 ms |
20468 KB |
Output is correct |
62 |
Correct |
367 ms |
18756 KB |
Output is correct |
63 |
Correct |
449 ms |
18740 KB |
Output is correct |
64 |
Correct |
1120 ms |
30736 KB |
Output is correct |
65 |
Correct |
974 ms |
34764 KB |
Output is correct |
66 |
Correct |
1220 ms |
57964 KB |
Output is correct |
67 |
Correct |
164 ms |
18748 KB |
Output is correct |
68 |
Correct |
579 ms |
30484 KB |
Output is correct |
69 |
Correct |
566 ms |
31244 KB |
Output is correct |
70 |
Correct |
525 ms |
29640 KB |
Output is correct |