#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
#define pb push_back
#define ins insert
#define fi first
#define se second
#define mod (ll)(1e9 + 7)
#define mxn (ll)(2e5 + 5)
#define inf (ll)(0x3F3F3F3F)
int n, k;
vector<pii> adj[mxn];
int sz[mxn];
int used[mxn];
inline void getsize(int a, int p)
{
sz[a] = 1;
for (pii v : adj[a])
{
if (used[v.fi] || v.fi == p) continue;
getsize(v.fi, a);
sz[a] += sz[v.fi];
}
}
inline int findcent(int a, int p, int &curn)
{
for (pii v : adj[a])
{
if (used[v.fi] || v.fi == p) continue;
if (sz[v.fi] > curn/2) return findcent(v.fi, a, curn);
}
return a;
}
vector<pii> arr;
pii mp[(int)(1e6 + 5)];
int res = inf;
int curc;
inline void init(int a, int p, int w, int w1)
{
if (k - w >= 0 && mp[k - w].se == curc)
{
res = min(res, mp[k - w].fi + w1);
}
arr.pb({w, w1});
for (pii v : adj[a])
{
if (used[v.fi] || v.fi == p) continue;
init(v.fi, a, w + v.se, w1 + 1);
}
}
inline void maincent(int a)
{
getsize(a, a);
int c = findcent(a, a, sz[a]);
curc = c;
mp[0] = {0, c};
for (pii v : adj[c])
{
if (used[v.fi]) continue;
arr.clear();
init(v.fi, c, v.se, 1);
for (pii x : arr)
{
if (x.fi > k) continue;
if (mp[x.fi].se != curc) mp[x.fi] = {x.se, curc};
else mp[x.fi].fi = min(mp[x.fi].fi, (int)x.se);
}
}
used[c] = 1;
for (pii v : adj[c])
{
if (used[v.fi]) continue;
maincent(v.fi);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for (int i = 0; i < (int)(1e6 + 5); i++) mp[i] = {0, -1};
n = N;
k = K;
for (int i = 0; i < n - 1; i++)
{
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
maincent(0);
return (res == inf ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18008 KB |
Output is correct |
2 |
Correct |
4 ms |
18008 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18008 KB |
Output is correct |
5 |
Correct |
4 ms |
18104 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18012 KB |
Output is correct |
14 |
Correct |
4 ms |
18144 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18264 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
3 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18008 KB |
Output is correct |
2 |
Correct |
4 ms |
18008 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18008 KB |
Output is correct |
5 |
Correct |
4 ms |
18104 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18012 KB |
Output is correct |
14 |
Correct |
4 ms |
18144 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18264 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
3 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
4 ms |
18012 KB |
Output is correct |
21 |
Correct |
4 ms |
18012 KB |
Output is correct |
22 |
Correct |
4 ms |
18264 KB |
Output is correct |
23 |
Correct |
4 ms |
18008 KB |
Output is correct |
24 |
Correct |
4 ms |
18008 KB |
Output is correct |
25 |
Correct |
5 ms |
18012 KB |
Output is correct |
26 |
Correct |
4 ms |
18012 KB |
Output is correct |
27 |
Correct |
4 ms |
18012 KB |
Output is correct |
28 |
Correct |
4 ms |
18012 KB |
Output is correct |
29 |
Correct |
4 ms |
18012 KB |
Output is correct |
30 |
Correct |
5 ms |
18148 KB |
Output is correct |
31 |
Correct |
4 ms |
18012 KB |
Output is correct |
32 |
Correct |
4 ms |
18012 KB |
Output is correct |
33 |
Correct |
4 ms |
18008 KB |
Output is correct |
34 |
Correct |
5 ms |
18012 KB |
Output is correct |
35 |
Correct |
4 ms |
18012 KB |
Output is correct |
36 |
Correct |
4 ms |
18008 KB |
Output is correct |
37 |
Correct |
4 ms |
18012 KB |
Output is correct |
38 |
Correct |
4 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18008 KB |
Output is correct |
2 |
Correct |
4 ms |
18008 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18008 KB |
Output is correct |
5 |
Correct |
4 ms |
18104 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18012 KB |
Output is correct |
14 |
Correct |
4 ms |
18144 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18264 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
3 ms |
18012 KB |
Output is correct |
19 |
Correct |
75 ms |
25848 KB |
Output is correct |
20 |
Correct |
78 ms |
25812 KB |
Output is correct |
21 |
Correct |
75 ms |
25856 KB |
Output is correct |
22 |
Correct |
67 ms |
26072 KB |
Output is correct |
23 |
Correct |
59 ms |
26324 KB |
Output is correct |
24 |
Correct |
42 ms |
25948 KB |
Output is correct |
25 |
Correct |
83 ms |
27148 KB |
Output is correct |
26 |
Correct |
64 ms |
28628 KB |
Output is correct |
27 |
Correct |
108 ms |
30600 KB |
Output is correct |
28 |
Correct |
205 ms |
37324 KB |
Output is correct |
29 |
Correct |
188 ms |
36968 KB |
Output is correct |
30 |
Correct |
115 ms |
30832 KB |
Output is correct |
31 |
Correct |
107 ms |
30800 KB |
Output is correct |
32 |
Correct |
124 ms |
30784 KB |
Output is correct |
33 |
Correct |
143 ms |
30668 KB |
Output is correct |
34 |
Correct |
148 ms |
31688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18008 KB |
Output is correct |
2 |
Correct |
4 ms |
18008 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18008 KB |
Output is correct |
5 |
Correct |
4 ms |
18104 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18012 KB |
Output is correct |
14 |
Correct |
4 ms |
18144 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18264 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
3 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
4 ms |
18012 KB |
Output is correct |
21 |
Correct |
4 ms |
18012 KB |
Output is correct |
22 |
Correct |
4 ms |
18264 KB |
Output is correct |
23 |
Correct |
4 ms |
18008 KB |
Output is correct |
24 |
Correct |
4 ms |
18008 KB |
Output is correct |
25 |
Correct |
5 ms |
18012 KB |
Output is correct |
26 |
Correct |
4 ms |
18012 KB |
Output is correct |
27 |
Correct |
4 ms |
18012 KB |
Output is correct |
28 |
Correct |
4 ms |
18012 KB |
Output is correct |
29 |
Correct |
4 ms |
18012 KB |
Output is correct |
30 |
Correct |
5 ms |
18148 KB |
Output is correct |
31 |
Correct |
4 ms |
18012 KB |
Output is correct |
32 |
Correct |
4 ms |
18012 KB |
Output is correct |
33 |
Correct |
4 ms |
18008 KB |
Output is correct |
34 |
Correct |
5 ms |
18012 KB |
Output is correct |
35 |
Correct |
4 ms |
18012 KB |
Output is correct |
36 |
Correct |
4 ms |
18008 KB |
Output is correct |
37 |
Correct |
4 ms |
18012 KB |
Output is correct |
38 |
Correct |
4 ms |
18012 KB |
Output is correct |
39 |
Correct |
75 ms |
25848 KB |
Output is correct |
40 |
Correct |
78 ms |
25812 KB |
Output is correct |
41 |
Correct |
75 ms |
25856 KB |
Output is correct |
42 |
Correct |
67 ms |
26072 KB |
Output is correct |
43 |
Correct |
59 ms |
26324 KB |
Output is correct |
44 |
Correct |
42 ms |
25948 KB |
Output is correct |
45 |
Correct |
83 ms |
27148 KB |
Output is correct |
46 |
Correct |
64 ms |
28628 KB |
Output is correct |
47 |
Correct |
108 ms |
30600 KB |
Output is correct |
48 |
Correct |
205 ms |
37324 KB |
Output is correct |
49 |
Correct |
188 ms |
36968 KB |
Output is correct |
50 |
Correct |
115 ms |
30832 KB |
Output is correct |
51 |
Correct |
107 ms |
30800 KB |
Output is correct |
52 |
Correct |
124 ms |
30784 KB |
Output is correct |
53 |
Correct |
143 ms |
30668 KB |
Output is correct |
54 |
Correct |
148 ms |
31688 KB |
Output is correct |
55 |
Correct |
8 ms |
18776 KB |
Output is correct |
56 |
Correct |
9 ms |
18800 KB |
Output is correct |
57 |
Correct |
52 ms |
26084 KB |
Output is correct |
58 |
Correct |
28 ms |
25348 KB |
Output is correct |
59 |
Correct |
64 ms |
28776 KB |
Output is correct |
60 |
Correct |
198 ms |
36928 KB |
Output is correct |
61 |
Correct |
106 ms |
30992 KB |
Output is correct |
62 |
Correct |
122 ms |
30920 KB |
Output is correct |
63 |
Correct |
123 ms |
30800 KB |
Output is correct |
64 |
Correct |
197 ms |
32144 KB |
Output is correct |
65 |
Correct |
179 ms |
32836 KB |
Output is correct |
66 |
Correct |
206 ms |
36044 KB |
Output is correct |
67 |
Correct |
70 ms |
31540 KB |
Output is correct |
68 |
Correct |
141 ms |
32712 KB |
Output is correct |
69 |
Correct |
138 ms |
32864 KB |
Output is correct |
70 |
Correct |
149 ms |
32200 KB |
Output is correct |