#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 2e5 + 5;
int k;
vpii adj[N];
int h[N];
ll d[N];
int sz[N];
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for (auto [v, w]: adj[u]){
if (v == p){
continue;
}
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int ans = INT_MAX;
gp_hash_table <ll, int> mpp[N];
void dfs_mergeset(int u, int p = -1){
int heavy = -1;
for (auto [v, w]: adj[u]){
if (v == p){
continue;
}
dfs_mergeset(v, u);
if (heavy == -1 or sz[heavy] < sz[v]){
heavy = v;
}
}
if (heavy != -1){
mpp[u].swap(mpp[heavy]);
}
for (auto [v, w]: adj[u]){
if (v == p or v == heavy){
continue;
}
for (auto [dpath, hpath]: mpp[v]){
auto itr = mpp[u].find(k + 2 * d[u] - dpath);
if (itr != mpp[u].end()){
ans = min(ans, hpath + itr->se - 2 * h[u]);
}
}
for (auto [dpath, hpath]: mpp[v]){
auto itr = mpp[u].find(dpath);
if (itr != mpp[u].end()){
itr->se = min(itr->se, hpath);
}
else{
mpp[u][dpath] = hpath;
}
}
}
{
auto itr = mpp[u].find(k + d[u]);
if (itr != mpp[u].end()){
ans = min(ans, itr->se - h[u]);
}
mpp[u][d[u]] = h[u];
}
}
int best_path(int n, int _k, int edge[][2], int weight[]){
k = _k;
For(i, 0, n - 1){
auto [u, v, w] = tuple{edge[i][0], edge[i][1], weight[i]};
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
dfs_sz(0);
dfs_mergeset(0);
if (ans == INT_MAX){
ans = -1;
}
return ans;
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
66100 KB |
Output is correct |
2 |
Correct |
43 ms |
66064 KB |
Output is correct |
3 |
Correct |
55 ms |
66012 KB |
Output is correct |
4 |
Correct |
44 ms |
66028 KB |
Output is correct |
5 |
Correct |
46 ms |
66100 KB |
Output is correct |
6 |
Correct |
44 ms |
65996 KB |
Output is correct |
7 |
Correct |
44 ms |
66008 KB |
Output is correct |
8 |
Correct |
48 ms |
65996 KB |
Output is correct |
9 |
Correct |
45 ms |
66100 KB |
Output is correct |
10 |
Correct |
43 ms |
66112 KB |
Output is correct |
11 |
Correct |
44 ms |
65996 KB |
Output is correct |
12 |
Correct |
46 ms |
66056 KB |
Output is correct |
13 |
Correct |
47 ms |
66032 KB |
Output is correct |
14 |
Correct |
47 ms |
66124 KB |
Output is correct |
15 |
Correct |
46 ms |
66052 KB |
Output is correct |
16 |
Correct |
45 ms |
66048 KB |
Output is correct |
17 |
Correct |
45 ms |
65996 KB |
Output is correct |
18 |
Correct |
44 ms |
65996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
66100 KB |
Output is correct |
2 |
Correct |
43 ms |
66064 KB |
Output is correct |
3 |
Correct |
55 ms |
66012 KB |
Output is correct |
4 |
Correct |
44 ms |
66028 KB |
Output is correct |
5 |
Correct |
46 ms |
66100 KB |
Output is correct |
6 |
Correct |
44 ms |
65996 KB |
Output is correct |
7 |
Correct |
44 ms |
66008 KB |
Output is correct |
8 |
Correct |
48 ms |
65996 KB |
Output is correct |
9 |
Correct |
45 ms |
66100 KB |
Output is correct |
10 |
Correct |
43 ms |
66112 KB |
Output is correct |
11 |
Correct |
44 ms |
65996 KB |
Output is correct |
12 |
Correct |
46 ms |
66056 KB |
Output is correct |
13 |
Correct |
47 ms |
66032 KB |
Output is correct |
14 |
Correct |
47 ms |
66124 KB |
Output is correct |
15 |
Correct |
46 ms |
66052 KB |
Output is correct |
16 |
Correct |
45 ms |
66048 KB |
Output is correct |
17 |
Correct |
45 ms |
65996 KB |
Output is correct |
18 |
Correct |
44 ms |
65996 KB |
Output is correct |
19 |
Correct |
46 ms |
66052 KB |
Output is correct |
20 |
Correct |
44 ms |
66072 KB |
Output is correct |
21 |
Correct |
46 ms |
66296 KB |
Output is correct |
22 |
Correct |
47 ms |
66316 KB |
Output is correct |
23 |
Correct |
46 ms |
66244 KB |
Output is correct |
24 |
Correct |
46 ms |
66256 KB |
Output is correct |
25 |
Correct |
46 ms |
66304 KB |
Output is correct |
26 |
Correct |
46 ms |
66328 KB |
Output is correct |
27 |
Correct |
45 ms |
66248 KB |
Output is correct |
28 |
Correct |
46 ms |
66324 KB |
Output is correct |
29 |
Correct |
46 ms |
66220 KB |
Output is correct |
30 |
Correct |
47 ms |
66252 KB |
Output is correct |
31 |
Correct |
50 ms |
66320 KB |
Output is correct |
32 |
Correct |
45 ms |
66320 KB |
Output is correct |
33 |
Correct |
46 ms |
66336 KB |
Output is correct |
34 |
Correct |
47 ms |
66212 KB |
Output is correct |
35 |
Correct |
47 ms |
66212 KB |
Output is correct |
36 |
Correct |
49 ms |
66252 KB |
Output is correct |
37 |
Correct |
47 ms |
66312 KB |
Output is correct |
38 |
Correct |
46 ms |
66188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
66100 KB |
Output is correct |
2 |
Correct |
43 ms |
66064 KB |
Output is correct |
3 |
Correct |
55 ms |
66012 KB |
Output is correct |
4 |
Correct |
44 ms |
66028 KB |
Output is correct |
5 |
Correct |
46 ms |
66100 KB |
Output is correct |
6 |
Correct |
44 ms |
65996 KB |
Output is correct |
7 |
Correct |
44 ms |
66008 KB |
Output is correct |
8 |
Correct |
48 ms |
65996 KB |
Output is correct |
9 |
Correct |
45 ms |
66100 KB |
Output is correct |
10 |
Correct |
43 ms |
66112 KB |
Output is correct |
11 |
Correct |
44 ms |
65996 KB |
Output is correct |
12 |
Correct |
46 ms |
66056 KB |
Output is correct |
13 |
Correct |
47 ms |
66032 KB |
Output is correct |
14 |
Correct |
47 ms |
66124 KB |
Output is correct |
15 |
Correct |
46 ms |
66052 KB |
Output is correct |
16 |
Correct |
45 ms |
66048 KB |
Output is correct |
17 |
Correct |
45 ms |
65996 KB |
Output is correct |
18 |
Correct |
44 ms |
65996 KB |
Output is correct |
19 |
Correct |
144 ms |
83164 KB |
Output is correct |
20 |
Correct |
157 ms |
83096 KB |
Output is correct |
21 |
Correct |
142 ms |
82912 KB |
Output is correct |
22 |
Correct |
167 ms |
82900 KB |
Output is correct |
23 |
Correct |
166 ms |
93896 KB |
Output is correct |
24 |
Correct |
154 ms |
89896 KB |
Output is correct |
25 |
Correct |
124 ms |
82712 KB |
Output is correct |
26 |
Correct |
94 ms |
88980 KB |
Output is correct |
27 |
Correct |
184 ms |
89676 KB |
Output is correct |
28 |
Correct |
269 ms |
130648 KB |
Output is correct |
29 |
Correct |
244 ms |
129176 KB |
Output is correct |
30 |
Correct |
188 ms |
89644 KB |
Output is correct |
31 |
Correct |
205 ms |
89616 KB |
Output is correct |
32 |
Correct |
255 ms |
89664 KB |
Output is correct |
33 |
Correct |
290 ms |
96144 KB |
Output is correct |
34 |
Correct |
347 ms |
129852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
66100 KB |
Output is correct |
2 |
Correct |
43 ms |
66064 KB |
Output is correct |
3 |
Correct |
55 ms |
66012 KB |
Output is correct |
4 |
Correct |
44 ms |
66028 KB |
Output is correct |
5 |
Correct |
46 ms |
66100 KB |
Output is correct |
6 |
Correct |
44 ms |
65996 KB |
Output is correct |
7 |
Correct |
44 ms |
66008 KB |
Output is correct |
8 |
Correct |
48 ms |
65996 KB |
Output is correct |
9 |
Correct |
45 ms |
66100 KB |
Output is correct |
10 |
Correct |
43 ms |
66112 KB |
Output is correct |
11 |
Correct |
44 ms |
65996 KB |
Output is correct |
12 |
Correct |
46 ms |
66056 KB |
Output is correct |
13 |
Correct |
47 ms |
66032 KB |
Output is correct |
14 |
Correct |
47 ms |
66124 KB |
Output is correct |
15 |
Correct |
46 ms |
66052 KB |
Output is correct |
16 |
Correct |
45 ms |
66048 KB |
Output is correct |
17 |
Correct |
45 ms |
65996 KB |
Output is correct |
18 |
Correct |
44 ms |
65996 KB |
Output is correct |
19 |
Correct |
46 ms |
66052 KB |
Output is correct |
20 |
Correct |
44 ms |
66072 KB |
Output is correct |
21 |
Correct |
46 ms |
66296 KB |
Output is correct |
22 |
Correct |
47 ms |
66316 KB |
Output is correct |
23 |
Correct |
46 ms |
66244 KB |
Output is correct |
24 |
Correct |
46 ms |
66256 KB |
Output is correct |
25 |
Correct |
46 ms |
66304 KB |
Output is correct |
26 |
Correct |
46 ms |
66328 KB |
Output is correct |
27 |
Correct |
45 ms |
66248 KB |
Output is correct |
28 |
Correct |
46 ms |
66324 KB |
Output is correct |
29 |
Correct |
46 ms |
66220 KB |
Output is correct |
30 |
Correct |
47 ms |
66252 KB |
Output is correct |
31 |
Correct |
50 ms |
66320 KB |
Output is correct |
32 |
Correct |
45 ms |
66320 KB |
Output is correct |
33 |
Correct |
46 ms |
66336 KB |
Output is correct |
34 |
Correct |
47 ms |
66212 KB |
Output is correct |
35 |
Correct |
47 ms |
66212 KB |
Output is correct |
36 |
Correct |
49 ms |
66252 KB |
Output is correct |
37 |
Correct |
47 ms |
66312 KB |
Output is correct |
38 |
Correct |
46 ms |
66188 KB |
Output is correct |
39 |
Correct |
144 ms |
83164 KB |
Output is correct |
40 |
Correct |
157 ms |
83096 KB |
Output is correct |
41 |
Correct |
142 ms |
82912 KB |
Output is correct |
42 |
Correct |
167 ms |
82900 KB |
Output is correct |
43 |
Correct |
166 ms |
93896 KB |
Output is correct |
44 |
Correct |
154 ms |
89896 KB |
Output is correct |
45 |
Correct |
124 ms |
82712 KB |
Output is correct |
46 |
Correct |
94 ms |
88980 KB |
Output is correct |
47 |
Correct |
184 ms |
89676 KB |
Output is correct |
48 |
Correct |
269 ms |
130648 KB |
Output is correct |
49 |
Correct |
244 ms |
129176 KB |
Output is correct |
50 |
Correct |
188 ms |
89644 KB |
Output is correct |
51 |
Correct |
205 ms |
89616 KB |
Output is correct |
52 |
Correct |
255 ms |
89664 KB |
Output is correct |
53 |
Correct |
290 ms |
96144 KB |
Output is correct |
54 |
Correct |
347 ms |
129852 KB |
Output is correct |
55 |
Correct |
58 ms |
68744 KB |
Output is correct |
56 |
Correct |
51 ms |
67348 KB |
Output is correct |
57 |
Correct |
105 ms |
81236 KB |
Output is correct |
58 |
Correct |
81 ms |
73792 KB |
Output is correct |
59 |
Correct |
102 ms |
98248 KB |
Output is correct |
60 |
Correct |
256 ms |
129772 KB |
Output is correct |
61 |
Correct |
210 ms |
93856 KB |
Output is correct |
62 |
Correct |
192 ms |
89652 KB |
Output is correct |
63 |
Correct |
268 ms |
89808 KB |
Output is correct |
64 |
Correct |
416 ms |
145320 KB |
Output is correct |
65 |
Correct |
402 ms |
142800 KB |
Output is correct |
66 |
Correct |
253 ms |
126428 KB |
Output is correct |
67 |
Correct |
225 ms |
82844 KB |
Output is correct |
68 |
Correct |
293 ms |
115180 KB |
Output is correct |
69 |
Correct |
298 ms |
115348 KB |
Output is correct |
70 |
Correct |
283 ms |
113676 KB |
Output is correct |