#include <bits/stdc++.h>
#include "race.h"
#define fi first
#define se second
#define pb push_back
#define ll long long
using namespace std;
typedef pair<ll,ll> pii;
typedef pair<ll, pii> ipii;
const ll MAXN = 2e5+10;
const ll INF = 2e18;
const ll MOD = 1e9+7;
ll n, k;
vector <pii> adj[MAXN];
ll sub[MAXN], big[MAXN], dep[MAXN], off[MAXN];
ll ans = INF;
map <ll, ll> *m[MAXN];
void dfs(ll nw, ll par){
sub[nw] = 1; dep[nw] = dep[par]+1;
big[nw] = n+1;
for(auto ed : adj[nw]){
ll nx = ed.fi;
if(nx == par) continue;
dfs(nx, nw);
if(sub[nx] > sub[big[nw]]) big[nw] = nx;
sub[nw] += sub[nx];
}
}
void upd(ll nw, ll len, ll depth){
if((*m[nw]).find(len) == (*m[nw]).end()) (*m[nw])[len] = depth;
else (*m[nw])[len] = min((*m[nw])[len], depth);
}
void dsu(ll nw, ll par){
if(adj[nw].size() == 1 && par != 0){ //leaf
m[nw] = new map<ll,ll>;
upd(nw, 0, dep[nw]);
return;
}
for(auto ed : adj[nw]){
ll nx = ed.fi; ll wei = ed.se;
if(nx == par) continue;
dsu(nx, nw); //dfs
off[nx] += wei; //nambah edge
}
m[nw] = m[big[nw]]; off[nw] = off[big[nw]]; //offset + map sama
upd(nw, -off[nw], dep[nw]);
for(auto ed : adj[nw]){
ll nx = ed.fi; ll wei = ed.se;
if(nx == par || nx == big[nw]) continue;
// cek inside
for(auto in : (*m[nx])){
ll len = in.fi; ll depth = in.se;
len += off[nx]; //keluar, nambahin
if(len > k || (*m[nw]).find(k-len-off[nw]) == (*m[nw]).end() ) continue; //gk ad
ans = min(ans, (*m[nw])[k-len-off[nw]] + depth - 2*dep[nw]);
//cek dalem, kurangin
}
// merge (*m[nx])
for(auto in : (*m[nx])){
ll len = in.fi; ll depth = in.se;
len += off[nx];
if( (*m[nw]).find(len-off[nw]) == (*m[nw]).end() ) (*m[nw])[len-off[nw]] = depth;
else (*m[nw])[len-off[nw]] = min((*m[nw])[len-off[nw]], depth);
}
}
// cout << nw << ' ' << off[nw] << "off\n";
// for(auto in : (*m[nw])){
// cout << in.fi << ' '<< in.se << '\n';
// }
//cek dalem
if( (*m[nw]).find(k-off[nw]) != (*m[nw]).end() )
ans = min(ans, (*m[nw])[k-off[nw]] - dep[nw]);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for(ll i=0; i<n-1; i++){
adj[H[i][0]+1].pb({H[i][1]+1, L[i]});
adj[H[i][1]+1].pb({H[i][0]+1, L[i]});
}
// 1-n
dfs(1, 0);
dsu(1, 0);
return (ans==INF ? -1 : ans);
}
Compilation message
race.cpp: In function 'void dsu(long long int, long long int)':
race.cpp:51:21: warning: unused variable 'wei' [-Wunused-variable]
51 | ll nx = ed.fi; ll wei = ed.se;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5000 KB |
Output is correct |
6 |
Correct |
2 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5000 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5008 KB |
Output is correct |
16 |
Correct |
2 ms |
5000 KB |
Output is correct |
17 |
Correct |
2 ms |
5000 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5000 KB |
Output is correct |
6 |
Correct |
2 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5000 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5008 KB |
Output is correct |
16 |
Correct |
2 ms |
5000 KB |
Output is correct |
17 |
Correct |
2 ms |
5000 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5008 KB |
Output is correct |
21 |
Correct |
3 ms |
5268 KB |
Output is correct |
22 |
Correct |
3 ms |
5400 KB |
Output is correct |
23 |
Correct |
3 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5272 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
3 ms |
5400 KB |
Output is correct |
27 |
Correct |
3 ms |
5140 KB |
Output is correct |
28 |
Correct |
3 ms |
5272 KB |
Output is correct |
29 |
Correct |
3 ms |
5356 KB |
Output is correct |
30 |
Correct |
3 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5332 KB |
Output is correct |
32 |
Correct |
3 ms |
5272 KB |
Output is correct |
33 |
Correct |
4 ms |
5396 KB |
Output is correct |
34 |
Correct |
3 ms |
5268 KB |
Output is correct |
35 |
Correct |
3 ms |
5204 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5000 KB |
Output is correct |
6 |
Correct |
2 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5000 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5008 KB |
Output is correct |
16 |
Correct |
2 ms |
5000 KB |
Output is correct |
17 |
Correct |
2 ms |
5000 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
81 ms |
30260 KB |
Output is correct |
20 |
Correct |
92 ms |
30232 KB |
Output is correct |
21 |
Correct |
79 ms |
30316 KB |
Output is correct |
22 |
Correct |
80 ms |
30924 KB |
Output is correct |
23 |
Correct |
107 ms |
44584 KB |
Output is correct |
24 |
Correct |
97 ms |
39036 KB |
Output is correct |
25 |
Correct |
69 ms |
25868 KB |
Output is correct |
26 |
Correct |
43 ms |
33288 KB |
Output is correct |
27 |
Correct |
140 ms |
47760 KB |
Output is correct |
28 |
Correct |
194 ms |
74400 KB |
Output is correct |
29 |
Correct |
198 ms |
72884 KB |
Output is correct |
30 |
Correct |
141 ms |
47828 KB |
Output is correct |
31 |
Correct |
141 ms |
47784 KB |
Output is correct |
32 |
Correct |
175 ms |
47956 KB |
Output is correct |
33 |
Correct |
158 ms |
40520 KB |
Output is correct |
34 |
Correct |
235 ms |
73188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5000 KB |
Output is correct |
6 |
Correct |
2 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5000 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5008 KB |
Output is correct |
16 |
Correct |
2 ms |
5000 KB |
Output is correct |
17 |
Correct |
2 ms |
5000 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5008 KB |
Output is correct |
21 |
Correct |
3 ms |
5268 KB |
Output is correct |
22 |
Correct |
3 ms |
5400 KB |
Output is correct |
23 |
Correct |
3 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5272 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
3 ms |
5400 KB |
Output is correct |
27 |
Correct |
3 ms |
5140 KB |
Output is correct |
28 |
Correct |
3 ms |
5272 KB |
Output is correct |
29 |
Correct |
3 ms |
5356 KB |
Output is correct |
30 |
Correct |
3 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5332 KB |
Output is correct |
32 |
Correct |
3 ms |
5272 KB |
Output is correct |
33 |
Correct |
4 ms |
5396 KB |
Output is correct |
34 |
Correct |
3 ms |
5268 KB |
Output is correct |
35 |
Correct |
3 ms |
5204 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5204 KB |
Output is correct |
39 |
Correct |
81 ms |
30260 KB |
Output is correct |
40 |
Correct |
92 ms |
30232 KB |
Output is correct |
41 |
Correct |
79 ms |
30316 KB |
Output is correct |
42 |
Correct |
80 ms |
30924 KB |
Output is correct |
43 |
Correct |
107 ms |
44584 KB |
Output is correct |
44 |
Correct |
97 ms |
39036 KB |
Output is correct |
45 |
Correct |
69 ms |
25868 KB |
Output is correct |
46 |
Correct |
43 ms |
33288 KB |
Output is correct |
47 |
Correct |
140 ms |
47760 KB |
Output is correct |
48 |
Correct |
194 ms |
74400 KB |
Output is correct |
49 |
Correct |
198 ms |
72884 KB |
Output is correct |
50 |
Correct |
141 ms |
47828 KB |
Output is correct |
51 |
Correct |
141 ms |
47784 KB |
Output is correct |
52 |
Correct |
175 ms |
47956 KB |
Output is correct |
53 |
Correct |
158 ms |
40520 KB |
Output is correct |
54 |
Correct |
235 ms |
73188 KB |
Output is correct |
55 |
Correct |
13 ms |
8576 KB |
Output is correct |
56 |
Correct |
8 ms |
7192 KB |
Output is correct |
57 |
Correct |
49 ms |
29712 KB |
Output is correct |
58 |
Correct |
44 ms |
28544 KB |
Output is correct |
59 |
Correct |
70 ms |
39624 KB |
Output is correct |
60 |
Correct |
206 ms |
73236 KB |
Output is correct |
61 |
Correct |
167 ms |
51288 KB |
Output is correct |
62 |
Correct |
146 ms |
48036 KB |
Output is correct |
63 |
Correct |
169 ms |
47932 KB |
Output is correct |
64 |
Correct |
352 ms |
97100 KB |
Output is correct |
65 |
Correct |
333 ms |
98828 KB |
Output is correct |
66 |
Correct |
202 ms |
69380 KB |
Output is correct |
67 |
Correct |
119 ms |
52784 KB |
Output is correct |
68 |
Correct |
247 ms |
67900 KB |
Output is correct |
69 |
Correct |
233 ms |
68444 KB |
Output is correct |
70 |
Correct |
212 ms |
65124 KB |
Output is correct |