#include "race.h"
#include <bits/stdc++.h>
///#include "grader.cpp"
using namespace std;
const int N_max = 2e5 + 2;
const int K_max = 1e6 + 2;
const int INF = 1e9 + 7;
vector < pair <int, int> > g[N_max];
bool vis[N_max];
int sz[N_max], k;
int ans;
int br[K_max];
void dfs1(int u, int p)
{
sz[u] = 1;
for (auto [v, w] : g[u])
{
if (v == p || vis[v])
{
continue;
}
dfs1(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int n)
{
for (auto [v, w] : g[u])
{
if (v == p || vis[v])
{
continue;
}
if (sz[v] * 2 > n)
{
return centroid(v, u, n);
}
}
return u;
}
void dfs2(int u, int p, int len, int d)
{
if (len > k)
{
return;
}
ans = min(ans, d + br[k - len]);
for (auto [v, w] : g[u])
{
if (v == p || vis[v])
{
continue;
}
dfs2(v, u, len + w, d + 1);
}
}
void dfs3(int u, int p, int len, int d, vector <int> &tmp)
{
if (len > k)
{
return;
}
tmp.push_back(len);
br[len] = min(br[len], d);
for (auto [v, w] : g[u])
{
if (v == p || vis[v])
{
continue;
}
dfs3(v, u, len + w, d + 1, tmp);
}
}
void solve(int u)
{
dfs1(u, -1);
vector <int> tmp;
for (auto [v, w] : g[u])
{
if (vis[v])
{
continue;
}
dfs2(v, u, w, 1);
dfs3(v, u, w, 1, tmp);
}
vis[u] = 1;
for (auto c : tmp)
{
br[c] = INF;
}
for (auto [v, w] : g[u])
{
if (vis[v])
{
continue;
}
solve(centroid(v, u, sz[v]));
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
ans = INF;
for (int i = 0; i < N - 1; i++)
{
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
for (int i = 1; i <= k; i++)
{
br[i] = INF;
}
dfs1(0, -1);
solve(centroid(0, -1, sz[0]));
if (ans == INF)
{
return -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5144 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5052 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
5140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5144 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5052 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
5140 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
8796 KB |
Output is correct |
23 |
Correct |
5 ms |
8028 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
6024 KB |
Output is correct |
29 |
Correct |
4 ms |
6492 KB |
Output is correct |
30 |
Correct |
4 ms |
6492 KB |
Output is correct |
31 |
Correct |
4 ms |
7772 KB |
Output is correct |
32 |
Correct |
5 ms |
7772 KB |
Output is correct |
33 |
Correct |
5 ms |
8252 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
4 ms |
8192 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
4 ms |
8284 KB |
Output is correct |
38 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5144 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5052 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
5140 KB |
Output is correct |
19 |
Correct |
85 ms |
11872 KB |
Output is correct |
20 |
Correct |
88 ms |
11860 KB |
Output is correct |
21 |
Correct |
86 ms |
12156 KB |
Output is correct |
22 |
Correct |
78 ms |
12464 KB |
Output is correct |
23 |
Correct |
55 ms |
12120 KB |
Output is correct |
24 |
Correct |
37 ms |
11868 KB |
Output is correct |
25 |
Correct |
88 ms |
15444 KB |
Output is correct |
26 |
Correct |
66 ms |
19888 KB |
Output is correct |
27 |
Correct |
96 ms |
18772 KB |
Output is correct |
28 |
Correct |
176 ms |
33184 KB |
Output is correct |
29 |
Correct |
155 ms |
32084 KB |
Output is correct |
30 |
Correct |
97 ms |
19112 KB |
Output is correct |
31 |
Correct |
113 ms |
18956 KB |
Output is correct |
32 |
Correct |
120 ms |
19000 KB |
Output is correct |
33 |
Correct |
137 ms |
17744 KB |
Output is correct |
34 |
Correct |
112 ms |
18516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5144 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5052 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
5140 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
8796 KB |
Output is correct |
23 |
Correct |
5 ms |
8028 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
6024 KB |
Output is correct |
29 |
Correct |
4 ms |
6492 KB |
Output is correct |
30 |
Correct |
4 ms |
6492 KB |
Output is correct |
31 |
Correct |
4 ms |
7772 KB |
Output is correct |
32 |
Correct |
5 ms |
7772 KB |
Output is correct |
33 |
Correct |
5 ms |
8252 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
4 ms |
8192 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
4 ms |
8284 KB |
Output is correct |
38 |
Correct |
4 ms |
7004 KB |
Output is correct |
39 |
Correct |
85 ms |
11872 KB |
Output is correct |
40 |
Correct |
88 ms |
11860 KB |
Output is correct |
41 |
Correct |
86 ms |
12156 KB |
Output is correct |
42 |
Correct |
78 ms |
12464 KB |
Output is correct |
43 |
Correct |
55 ms |
12120 KB |
Output is correct |
44 |
Correct |
37 ms |
11868 KB |
Output is correct |
45 |
Correct |
88 ms |
15444 KB |
Output is correct |
46 |
Correct |
66 ms |
19888 KB |
Output is correct |
47 |
Correct |
96 ms |
18772 KB |
Output is correct |
48 |
Correct |
176 ms |
33184 KB |
Output is correct |
49 |
Correct |
155 ms |
32084 KB |
Output is correct |
50 |
Correct |
97 ms |
19112 KB |
Output is correct |
51 |
Correct |
113 ms |
18956 KB |
Output is correct |
52 |
Correct |
120 ms |
19000 KB |
Output is correct |
53 |
Correct |
137 ms |
17744 KB |
Output is correct |
54 |
Correct |
112 ms |
18516 KB |
Output is correct |
55 |
Correct |
8 ms |
5720 KB |
Output is correct |
56 |
Correct |
9 ms |
5736 KB |
Output is correct |
57 |
Correct |
51 ms |
12612 KB |
Output is correct |
58 |
Correct |
24 ms |
12496 KB |
Output is correct |
59 |
Correct |
65 ms |
20636 KB |
Output is correct |
60 |
Correct |
272 ms |
38020 KB |
Output is correct |
61 |
Correct |
106 ms |
19388 KB |
Output is correct |
62 |
Correct |
122 ms |
23760 KB |
Output is correct |
63 |
Correct |
154 ms |
23884 KB |
Output is correct |
64 |
Correct |
255 ms |
22728 KB |
Output is correct |
65 |
Correct |
130 ms |
19768 KB |
Output is correct |
66 |
Correct |
232 ms |
33800 KB |
Output is correct |
67 |
Correct |
62 ms |
25168 KB |
Output is correct |
68 |
Correct |
154 ms |
24524 KB |
Output is correct |
69 |
Correct |
143 ms |
24912 KB |
Output is correct |
70 |
Correct |
126 ms |
24008 KB |
Output is correct |