//#include "race.h"
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define F first
#define S second
#define pii pair<ll,ll>
#define mpr make_pair
typedef long long ll;
const int maxn = 2e5+10;
const int inf = 1e9+10;
const int mod = 1e9+7;
ll K;
map<ll,ll> *mp[maxn];
ll st[maxn], h[maxn], sum[maxn];
vector<pii> g[maxn];
void pfs(ll v, ll p = -1)
{
st[v] = 1;
for(auto e : g[v])
{
ll u = e.F, w = e.S;
if(u != p)
{
sum[u] = sum[v] + w;
h[u] = h[v] + 1;
pfs(u,v);
st[v] += st[u];
}
}
}
ll ans = inf;
void dfs(ll v, ll p = -1)
{
ll big = -1;
for(auto e : g[v])
{
int u = e.F;
if(u != p)
{
dfs(u,v);
if(big == -1) big = u;
else if(st[u] > st[big]) big = u;
}
}
if(big != -1) mp[v] = mp[big];
else mp[v] = new map<ll,ll>;
(*mp[v])[sum[v]] = h[v];
if((*mp[v]).count(K+sum[v]))
ans = min(ans, (*mp[v])[K+sum[v]] - h[v]);
for(auto e : g[v])
{
ll u = e.F, w = e.S;
if(u != p && u != big)
{
for(auto X : (*mp[u]))
{
ll sm = X.F, Mn = X.S;
if((*mp[v]).count(K-sm+2*sum[v]))
{
ll road = Mn + (*mp[v])[K-sm+2*sum[v]] - 2 * h[v];
ans = min(ans,road);
}
}
for(auto X : (*mp[u]))
{
ll sm = X.F, Mn = X.S;
if(!(*mp[v]).count(sm)) (*mp[v])[sm] = Mn;
else (*mp[v])[sm] = min((*mp[v])[sm], Mn);
}
}
}
}
int best_path(int n, int k, int h[][2], int l[])
{
//cin>> n >> K;
K = k;
for(int i = 0, u, v, w; i < n-1; i++)
{
//cin>> u >> v >> w;
u = h[i][0], v = h[i][1], w = l[i];
u++; v++;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
pfs(1); dfs(1);
if(ans == inf) return -1;
else return ans;
}
/*int main()
{
}*/
Compilation message
race.cpp: In function 'void dfs(ll, ll)':
race.cpp:64:19: warning: unused variable 'w' [-Wunused-variable]
64 | ll u = e.F, w = e.S;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5376 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
6 ms |
5376 KB |
Output is correct |
24 |
Correct |
5 ms |
5376 KB |
Output is correct |
25 |
Correct |
6 ms |
5376 KB |
Output is correct |
26 |
Correct |
5 ms |
5376 KB |
Output is correct |
27 |
Correct |
5 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5376 KB |
Output is correct |
29 |
Correct |
5 ms |
5376 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
5 ms |
5376 KB |
Output is correct |
32 |
Correct |
5 ms |
5376 KB |
Output is correct |
33 |
Correct |
5 ms |
5376 KB |
Output is correct |
34 |
Correct |
5 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
157 ms |
28300 KB |
Output is correct |
20 |
Correct |
158 ms |
29560 KB |
Output is correct |
21 |
Correct |
159 ms |
29688 KB |
Output is correct |
22 |
Correct |
160 ms |
30200 KB |
Output is correct |
23 |
Correct |
191 ms |
44024 KB |
Output is correct |
24 |
Correct |
177 ms |
38264 KB |
Output is correct |
25 |
Correct |
105 ms |
23672 KB |
Output is correct |
26 |
Correct |
63 ms |
29560 KB |
Output is correct |
27 |
Correct |
258 ms |
46328 KB |
Output is correct |
28 |
Correct |
285 ms |
66912 KB |
Output is correct |
29 |
Correct |
290 ms |
65656 KB |
Output is correct |
30 |
Correct |
271 ms |
46460 KB |
Output is correct |
31 |
Correct |
260 ms |
46328 KB |
Output is correct |
32 |
Correct |
336 ms |
46456 KB |
Output is correct |
33 |
Correct |
287 ms |
38904 KB |
Output is correct |
34 |
Correct |
405 ms |
71800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5376 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
6 ms |
5376 KB |
Output is correct |
24 |
Correct |
5 ms |
5376 KB |
Output is correct |
25 |
Correct |
6 ms |
5376 KB |
Output is correct |
26 |
Correct |
5 ms |
5376 KB |
Output is correct |
27 |
Correct |
5 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5376 KB |
Output is correct |
29 |
Correct |
5 ms |
5376 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
5 ms |
5376 KB |
Output is correct |
32 |
Correct |
5 ms |
5376 KB |
Output is correct |
33 |
Correct |
5 ms |
5376 KB |
Output is correct |
34 |
Correct |
5 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5248 KB |
Output is correct |
39 |
Correct |
157 ms |
28300 KB |
Output is correct |
40 |
Correct |
158 ms |
29560 KB |
Output is correct |
41 |
Correct |
159 ms |
29688 KB |
Output is correct |
42 |
Correct |
160 ms |
30200 KB |
Output is correct |
43 |
Correct |
191 ms |
44024 KB |
Output is correct |
44 |
Correct |
177 ms |
38264 KB |
Output is correct |
45 |
Correct |
105 ms |
23672 KB |
Output is correct |
46 |
Correct |
63 ms |
29560 KB |
Output is correct |
47 |
Correct |
258 ms |
46328 KB |
Output is correct |
48 |
Correct |
285 ms |
66912 KB |
Output is correct |
49 |
Correct |
290 ms |
65656 KB |
Output is correct |
50 |
Correct |
271 ms |
46460 KB |
Output is correct |
51 |
Correct |
260 ms |
46328 KB |
Output is correct |
52 |
Correct |
336 ms |
46456 KB |
Output is correct |
53 |
Correct |
287 ms |
38904 KB |
Output is correct |
54 |
Correct |
405 ms |
71800 KB |
Output is correct |
55 |
Correct |
20 ms |
8608 KB |
Output is correct |
56 |
Correct |
12 ms |
7168 KB |
Output is correct |
57 |
Correct |
99 ms |
29092 KB |
Output is correct |
58 |
Correct |
74 ms |
27624 KB |
Output is correct |
59 |
Correct |
90 ms |
35832 KB |
Output is correct |
60 |
Correct |
269 ms |
65784 KB |
Output is correct |
61 |
Correct |
294 ms |
49784 KB |
Output is correct |
62 |
Correct |
255 ms |
46328 KB |
Output is correct |
63 |
Correct |
329 ms |
46456 KB |
Output is correct |
64 |
Correct |
569 ms |
95736 KB |
Output is correct |
65 |
Correct |
562 ms |
97272 KB |
Output is correct |
66 |
Correct |
297 ms |
63096 KB |
Output is correct |
67 |
Correct |
265 ms |
51296 KB |
Output is correct |
68 |
Correct |
453 ms |
66552 KB |
Output is correct |
69 |
Correct |
437 ms |
67192 KB |
Output is correct |
70 |
Correct |
463 ms |
63736 KB |
Output is correct |