#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
const int MAXN=200005;
typedef pair<int,int> pii;
long long pref[MAXN];
vector<pii> adj[MAXN];
int depth[MAXN],KK,ans=1e9;
map<long long,int> subtree[MAXN];
void merge(int now,int nxt){
for(map<long long,int>::iterator it=subtree[nxt].begin();it!=subtree[nxt].end();++it){
if(subtree[now].count(it->first))subtree[now][it->first]=min(subtree[now][it->first],it->second);
else subtree[now][it->first]=it->second;
}
}
void dfs(int now,int par){
subtree[now][pref[now]]=depth[now];
//hitung semua pasangan node yang LCAnya now
for(auto nxt : adj[now]){
if(nxt.fi==par)continue;
depth[nxt.fi]=depth[now]+1;
pref[nxt.fi]=pref[now]+nxt.se;
dfs(nxt.fi,now);
if(subtree[now].size()<subtree[nxt.fi].size())subtree[now].swap(subtree[nxt.fi]);
}
for(auto nxt : adj[now]){
if(nxt.fi==par)continue;
for(map<long long,int>::iterator it=subtree[nxt.fi].begin();it!=subtree[nxt.fi].end();++it){
int sisa=KK+2*pref[now]-it->first;
if(subtree[now].count(sisa)){
ans=min(ans,it->second+subtree[now][sisa]-2*depth[now]);
}
}
merge(now,nxt.fi);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
KK=K;
for(int i=0;i<N-1;i++){
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
dfs(0,-1);
if(ans==1e9)ans=-1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
12 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
12 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14700 KB |
Output is correct |
22 |
Correct |
12 ms |
14828 KB |
Output is correct |
23 |
Correct |
11 ms |
14696 KB |
Output is correct |
24 |
Correct |
12 ms |
14700 KB |
Output is correct |
25 |
Correct |
12 ms |
14720 KB |
Output is correct |
26 |
Correct |
12 ms |
14700 KB |
Output is correct |
27 |
Correct |
11 ms |
14572 KB |
Output is correct |
28 |
Correct |
12 ms |
14700 KB |
Output is correct |
29 |
Correct |
12 ms |
14700 KB |
Output is correct |
30 |
Correct |
14 ms |
14700 KB |
Output is correct |
31 |
Correct |
13 ms |
14700 KB |
Output is correct |
32 |
Correct |
11 ms |
14700 KB |
Output is correct |
33 |
Correct |
12 ms |
14828 KB |
Output is correct |
34 |
Correct |
11 ms |
14700 KB |
Output is correct |
35 |
Correct |
13 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14732 KB |
Output is correct |
37 |
Correct |
11 ms |
14700 KB |
Output is correct |
38 |
Correct |
11 ms |
14700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
12 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
158 ms |
36452 KB |
Output is correct |
20 |
Correct |
159 ms |
36452 KB |
Output is correct |
21 |
Correct |
152 ms |
36324 KB |
Output is correct |
22 |
Correct |
165 ms |
36196 KB |
Output is correct |
23 |
Correct |
219 ms |
49444 KB |
Output is correct |
24 |
Correct |
171 ms |
40180 KB |
Output is correct |
25 |
Correct |
152 ms |
36452 KB |
Output is correct |
26 |
Correct |
76 ms |
43232 KB |
Output is correct |
27 |
Correct |
262 ms |
48612 KB |
Output is correct |
28 |
Correct |
384 ms |
84708 KB |
Output is correct |
29 |
Correct |
378 ms |
83428 KB |
Output is correct |
30 |
Correct |
250 ms |
48868 KB |
Output is correct |
31 |
Correct |
254 ms |
48868 KB |
Output is correct |
32 |
Correct |
342 ms |
48740 KB |
Output is correct |
33 |
Correct |
321 ms |
53600 KB |
Output is correct |
34 |
Correct |
437 ms |
86500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
12 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14700 KB |
Output is correct |
22 |
Correct |
12 ms |
14828 KB |
Output is correct |
23 |
Correct |
11 ms |
14696 KB |
Output is correct |
24 |
Correct |
12 ms |
14700 KB |
Output is correct |
25 |
Correct |
12 ms |
14720 KB |
Output is correct |
26 |
Correct |
12 ms |
14700 KB |
Output is correct |
27 |
Correct |
11 ms |
14572 KB |
Output is correct |
28 |
Correct |
12 ms |
14700 KB |
Output is correct |
29 |
Correct |
12 ms |
14700 KB |
Output is correct |
30 |
Correct |
14 ms |
14700 KB |
Output is correct |
31 |
Correct |
13 ms |
14700 KB |
Output is correct |
32 |
Correct |
11 ms |
14700 KB |
Output is correct |
33 |
Correct |
12 ms |
14828 KB |
Output is correct |
34 |
Correct |
11 ms |
14700 KB |
Output is correct |
35 |
Correct |
13 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14732 KB |
Output is correct |
37 |
Correct |
11 ms |
14700 KB |
Output is correct |
38 |
Correct |
11 ms |
14700 KB |
Output is correct |
39 |
Correct |
158 ms |
36452 KB |
Output is correct |
40 |
Correct |
159 ms |
36452 KB |
Output is correct |
41 |
Correct |
152 ms |
36324 KB |
Output is correct |
42 |
Correct |
165 ms |
36196 KB |
Output is correct |
43 |
Correct |
219 ms |
49444 KB |
Output is correct |
44 |
Correct |
171 ms |
40180 KB |
Output is correct |
45 |
Correct |
152 ms |
36452 KB |
Output is correct |
46 |
Correct |
76 ms |
43232 KB |
Output is correct |
47 |
Correct |
262 ms |
48612 KB |
Output is correct |
48 |
Correct |
384 ms |
84708 KB |
Output is correct |
49 |
Correct |
378 ms |
83428 KB |
Output is correct |
50 |
Correct |
250 ms |
48868 KB |
Output is correct |
51 |
Correct |
254 ms |
48868 KB |
Output is correct |
52 |
Correct |
342 ms |
48740 KB |
Output is correct |
53 |
Correct |
321 ms |
53600 KB |
Output is correct |
54 |
Correct |
437 ms |
86500 KB |
Output is correct |
55 |
Correct |
29 ms |
17644 KB |
Output is correct |
56 |
Correct |
19 ms |
16364 KB |
Output is correct |
57 |
Correct |
101 ms |
34404 KB |
Output is correct |
58 |
Correct |
69 ms |
27992 KB |
Output is correct |
59 |
Correct |
116 ms |
49636 KB |
Output is correct |
60 |
Correct |
387 ms |
83684 KB |
Output is correct |
61 |
Correct |
310 ms |
52232 KB |
Output is correct |
62 |
Correct |
297 ms |
48740 KB |
Output is correct |
63 |
Correct |
369 ms |
48740 KB |
Output is correct |
64 |
Correct |
560 ms |
100452 KB |
Output is correct |
65 |
Correct |
577 ms |
100328 KB |
Output is correct |
66 |
Correct |
397 ms |
80516 KB |
Output is correct |
67 |
Correct |
228 ms |
43288 KB |
Output is correct |
68 |
Correct |
460 ms |
65380 KB |
Output is correct |
69 |
Correct |
461 ms |
65992 KB |
Output is correct |
70 |
Correct |
440 ms |
63332 KB |
Output is correct |