#include "race.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
///Sack
///Determine the minimum length of a path that has sum k
struct edge
{
int to;
ll w;
};
vector<edge> adjList[200005];
int Size[200005];
int getSize(int u,int p)
{
Size[u]=1;
for(edge e: adjList[u]){
int v=e.to;
if(v==p) continue;
Size[u]+=getSize(v,u);
}
return Size[u];
}
ll k;
ll minimum=INT_MAX;
map<ll,int>* sack[200005]; //<distance from the root, depth>
typedef pair<ll,int> pii;
void dfs(int u,int p,ll distRoot,int depth)
{
int greatest=0,bigChild=-1;
for(edge e: adjList[u]){
int v=e.to;
if(v==p) continue;
dfs(v,u,distRoot+e.w,depth+1);
if(Size[v]>greatest){
greatest=Size[v];
bigChild=v;
}
}
if(bigChild==-1)
sack[u]=new map<ll,int>;
else
sack[u]=sack[bigChild];
for(edge e: adjList[u]){
int v=e.to;
if(v==p) continue;
if(v==bigChild) continue;
for(pii x: *sack[v]){ //Combine two lines in that subtree
ll dist=x.first-distRoot;
ll complement=k-dist+distRoot;
if(complement>=0 && (*sack[u]).find(complement)!=(*sack[u]).end()){
ll length=(*sack[u])[complement]+x.second;
length-=(ll)2*depth;
minimum=min(minimum,length);
}
}
for(pii x: *sack[v]){ //Copy information from child
if((*sack[u])[x.first]==0) (*sack[u])[x.first]=x.second;
else{
int length=(*sack[u])[x.first];
(*sack[u])[x.first]=min(length,x.second);
}
}
}
if((*sack[u]).find(k+distRoot)!=(*sack[u]).end()){ //Check if a line has sum k
ll length=(*sack[u])[k+distRoot];
length-=depth;
minimum=min(minimum,length);
}
if((*sack[u])[distRoot]==0) (*sack[u])[distRoot]=depth; //Add line from root to u to the sack
else{
int length=(*sack[u])[distRoot];
(*sack[u])[distRoot]=min(length,depth);
}
}
int best_path(int n, int K, int H[][2], int L[])
{
k=K;
for(int i=0;i<n-1;i++){
int a=H[i][0]+1;
int b=H[i][1]+1;
ll w=L[i];
adjList[a].push_back({b,w});
adjList[b].push_back({a,w});
}
getSize(1,0);
dfs(1,0,0,0);
return (minimum!=INT_MAX)? minimum : -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
8 ms |
5376 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
8 ms |
5376 KB |
Output is correct |
25 |
Correct |
8 ms |
5376 KB |
Output is correct |
26 |
Correct |
8 ms |
5376 KB |
Output is correct |
27 |
Correct |
8 ms |
5248 KB |
Output is correct |
28 |
Correct |
8 ms |
5376 KB |
Output is correct |
29 |
Correct |
8 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5376 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5376 KB |
Output is correct |
33 |
Correct |
8 ms |
5376 KB |
Output is correct |
34 |
Correct |
8 ms |
5248 KB |
Output is correct |
35 |
Correct |
8 ms |
5248 KB |
Output is correct |
36 |
Correct |
8 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5248 KB |
Output is correct |
38 |
Correct |
8 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
144 ms |
26360 KB |
Output is correct |
20 |
Correct |
132 ms |
26464 KB |
Output is correct |
21 |
Correct |
127 ms |
26488 KB |
Output is correct |
22 |
Correct |
150 ms |
27000 KB |
Output is correct |
23 |
Correct |
165 ms |
40696 KB |
Output is correct |
24 |
Correct |
134 ms |
34996 KB |
Output is correct |
25 |
Correct |
104 ms |
22776 KB |
Output is correct |
26 |
Correct |
68 ms |
31096 KB |
Output is correct |
27 |
Correct |
224 ms |
39800 KB |
Output is correct |
28 |
Correct |
296 ms |
69368 KB |
Output is correct |
29 |
Correct |
299 ms |
67576 KB |
Output is correct |
30 |
Correct |
231 ms |
39816 KB |
Output is correct |
31 |
Correct |
250 ms |
39724 KB |
Output is correct |
32 |
Correct |
270 ms |
39872 KB |
Output is correct |
33 |
Correct |
235 ms |
32376 KB |
Output is correct |
34 |
Correct |
352 ms |
64376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
8 ms |
5376 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
8 ms |
5376 KB |
Output is correct |
25 |
Correct |
8 ms |
5376 KB |
Output is correct |
26 |
Correct |
8 ms |
5376 KB |
Output is correct |
27 |
Correct |
8 ms |
5248 KB |
Output is correct |
28 |
Correct |
8 ms |
5376 KB |
Output is correct |
29 |
Correct |
8 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5376 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5376 KB |
Output is correct |
33 |
Correct |
8 ms |
5376 KB |
Output is correct |
34 |
Correct |
8 ms |
5248 KB |
Output is correct |
35 |
Correct |
8 ms |
5248 KB |
Output is correct |
36 |
Correct |
8 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5248 KB |
Output is correct |
38 |
Correct |
8 ms |
5248 KB |
Output is correct |
39 |
Correct |
144 ms |
26360 KB |
Output is correct |
40 |
Correct |
132 ms |
26464 KB |
Output is correct |
41 |
Correct |
127 ms |
26488 KB |
Output is correct |
42 |
Correct |
150 ms |
27000 KB |
Output is correct |
43 |
Correct |
165 ms |
40696 KB |
Output is correct |
44 |
Correct |
134 ms |
34996 KB |
Output is correct |
45 |
Correct |
104 ms |
22776 KB |
Output is correct |
46 |
Correct |
68 ms |
31096 KB |
Output is correct |
47 |
Correct |
224 ms |
39800 KB |
Output is correct |
48 |
Correct |
296 ms |
69368 KB |
Output is correct |
49 |
Correct |
299 ms |
67576 KB |
Output is correct |
50 |
Correct |
231 ms |
39816 KB |
Output is correct |
51 |
Correct |
250 ms |
39724 KB |
Output is correct |
52 |
Correct |
270 ms |
39872 KB |
Output is correct |
53 |
Correct |
235 ms |
32376 KB |
Output is correct |
54 |
Correct |
352 ms |
64376 KB |
Output is correct |
55 |
Correct |
22 ms |
8320 KB |
Output is correct |
56 |
Correct |
15 ms |
6912 KB |
Output is correct |
57 |
Correct |
77 ms |
25976 KB |
Output is correct |
58 |
Correct |
71 ms |
24856 KB |
Output is correct |
59 |
Correct |
100 ms |
37368 KB |
Output is correct |
60 |
Correct |
273 ms |
68216 KB |
Output is correct |
61 |
Correct |
259 ms |
43128 KB |
Output is correct |
62 |
Correct |
232 ms |
39768 KB |
Output is correct |
63 |
Correct |
277 ms |
39800 KB |
Output is correct |
64 |
Correct |
495 ms |
88568 KB |
Output is correct |
65 |
Correct |
490 ms |
89848 KB |
Output is correct |
66 |
Correct |
295 ms |
63356 KB |
Output is correct |
67 |
Correct |
186 ms |
44508 KB |
Output is correct |
68 |
Correct |
363 ms |
59128 KB |
Output is correct |
69 |
Correct |
348 ms |
59768 KB |
Output is correct |
70 |
Correct |
333 ms |
56952 KB |
Output is correct |