#include "race.h"
#include<iostream>
#include<utility>
#include<vector>
#include<map>
#include <cstring>
using namespace std;
const long long N = 2e5+5;
long long sz[N];
long long st[N];
long long ft[N];
long long ver[N];
long long orig_len[N];
long long orig_sum[N];
map<long long,long long> mem;
vector<pair<long long,long long>> g[N];
long long t;
long long ans = 1e9;
bool vis[N];
void preprocess(long long u,long long len,long long sum,long long p){
if(vis[u]) return;
vis[u] = true;
++t;
st[u] = t;
ver[t] = u;
orig_sum[u] = sum;
orig_len[u] = len;
sz[u] = 1;
for(auto &x:g[u]){
if(x.first!=p){
preprocess(x.first,len+1,sum+x.second,u);
sz[u]+=sz[x.first];
}
}
ft[u] = t;
}
void solve(long long u,long long target,long long p,bool keep){
if(vis[u]) return;
vis[u] = true;
long long mx = -1,big_child = -1,del_sum = orig_sum[u],del_len = orig_len[u];
for(auto &x:g[u]) if(x.first!=p&&sz[x.first]>mx) mx = sz[x.first],big_child = x.first;
for(auto &x:g[u]){
if(x.first!=p&&x.first!=big_child){
solve(x.first,target,u,false);
}
}
if(big_child!=-1){
solve(big_child,target,u,true);
}
long long need = target-orig_sum[u]+2*del_sum;
if(need>=0&&mem.find(need)!=mem.end()) ans = min(ans,mem[need]+orig_len[u]-2*del_len);
if(mem.find(orig_sum[u])==mem.end()) mem[orig_sum[u]] = orig_len[u];
mem[orig_sum[u]] = min(mem[orig_sum[u]],orig_len[u]);
for(auto &x:g[u]){
if(x.first!=p&&x.first!=big_child){
for(long long i=st[x.first];i<=ft[x.first];i++){
long long v = ver[i];
need = target-orig_sum[v]+2*del_sum;
if(need>=0&&mem.find(need)!=mem.end()) ans = min(ans,mem[need]+orig_len[v]-2*del_len);
}
for(long long i=st[x.first];i<=ft[x.first];i++){
long long v = ver[i];
if(mem.find(orig_sum[v])==mem.end()) mem[orig_sum[v]] = orig_len[v];
mem[orig_sum[v]] = min(mem[orig_sum[v]],orig_len[v]);
}
}
}
if(!keep){
for(long long i=st[u];i<=ft[u];i++){
if(mem.find(orig_sum[ver[i]])!=mem.end()) mem.erase(orig_sum[ver[i]]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(long long i=0;i<N;i++) g[H[i][0]].push_back({H[i][1],L[i]}),g[H[i][1]].push_back({H[i][0],L[i]});
preprocess(0,0,0,0);
memset(vis,false,sizeof vis);
solve(0,K,0,true);
if(ans>=N) return -1;
return ans;
}
//int n, K;
//int H[N][2];
//int L[N];
//void read_input()
//{
// long long i;
// scanf("%d %d",&n,&K);
// for(i=0; i<n-1; i++)
// scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]);
//}
//int main(){
// int ans;
// read_input();
// ans = best_path(n,K,H,L);
// for(int i=0;i<n;i++) cout << st[i] << " " << ft[i] << "\n";
// cout << ans << "\n";
// return 0;
//}
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
3 3
0 1 1
1 2 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5264 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5284 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5264 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5284 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
5 ms |
5372 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
5 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5288 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5364 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
5 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
4 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5408 KB |
Output is correct |
36 |
Correct |
4 ms |
5468 KB |
Output is correct |
37 |
Correct |
6 ms |
5368 KB |
Output is correct |
38 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5264 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5284 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
165 ms |
16220 KB |
Output is correct |
20 |
Correct |
163 ms |
16212 KB |
Output is correct |
21 |
Correct |
176 ms |
16220 KB |
Output is correct |
22 |
Correct |
160 ms |
16240 KB |
Output is correct |
23 |
Correct |
293 ms |
16716 KB |
Output is correct |
24 |
Correct |
165 ms |
15936 KB |
Output is correct |
25 |
Correct |
94 ms |
26000 KB |
Output is correct |
26 |
Correct |
55 ms |
34600 KB |
Output is correct |
27 |
Correct |
200 ms |
27852 KB |
Output is correct |
28 |
Correct |
379 ms |
76196 KB |
Output is correct |
29 |
Correct |
365 ms |
73476 KB |
Output is correct |
30 |
Correct |
206 ms |
27812 KB |
Output is correct |
31 |
Correct |
192 ms |
27920 KB |
Output is correct |
32 |
Correct |
316 ms |
28008 KB |
Output is correct |
33 |
Correct |
279 ms |
26976 KB |
Output is correct |
34 |
Correct |
518 ms |
39240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5264 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5284 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
5 ms |
5372 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
5 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5288 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5364 KB |
Output is correct |
30 |
Correct |
5 ms |
5332 KB |
Output is correct |
31 |
Correct |
5 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
4 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5408 KB |
Output is correct |
36 |
Correct |
4 ms |
5468 KB |
Output is correct |
37 |
Correct |
6 ms |
5368 KB |
Output is correct |
38 |
Correct |
5 ms |
5324 KB |
Output is correct |
39 |
Correct |
165 ms |
16220 KB |
Output is correct |
40 |
Correct |
163 ms |
16212 KB |
Output is correct |
41 |
Correct |
176 ms |
16220 KB |
Output is correct |
42 |
Correct |
160 ms |
16240 KB |
Output is correct |
43 |
Correct |
293 ms |
16716 KB |
Output is correct |
44 |
Correct |
165 ms |
15936 KB |
Output is correct |
45 |
Correct |
94 ms |
26000 KB |
Output is correct |
46 |
Correct |
55 ms |
34600 KB |
Output is correct |
47 |
Correct |
200 ms |
27852 KB |
Output is correct |
48 |
Correct |
379 ms |
76196 KB |
Output is correct |
49 |
Correct |
365 ms |
73476 KB |
Output is correct |
50 |
Correct |
206 ms |
27812 KB |
Output is correct |
51 |
Correct |
192 ms |
27920 KB |
Output is correct |
52 |
Correct |
316 ms |
28008 KB |
Output is correct |
53 |
Correct |
279 ms |
26976 KB |
Output is correct |
54 |
Correct |
518 ms |
39240 KB |
Output is correct |
55 |
Correct |
19 ms |
6688 KB |
Output is correct |
56 |
Correct |
13 ms |
6356 KB |
Output is correct |
57 |
Correct |
106 ms |
16572 KB |
Output is correct |
58 |
Correct |
54 ms |
16952 KB |
Output is correct |
59 |
Correct |
115 ms |
42140 KB |
Output is correct |
60 |
Correct |
334 ms |
77384 KB |
Output is correct |
61 |
Correct |
273 ms |
32580 KB |
Output is correct |
62 |
Correct |
230 ms |
30952 KB |
Output is correct |
63 |
Correct |
377 ms |
31048 KB |
Output is correct |
64 |
Correct |
910 ms |
36984 KB |
Output is correct |
65 |
Correct |
862 ms |
43492 KB |
Output is correct |
66 |
Correct |
353 ms |
70448 KB |
Output is correct |
67 |
Correct |
191 ms |
29716 KB |
Output is correct |
68 |
Correct |
398 ms |
41372 KB |
Output is correct |
69 |
Correct |
418 ms |
41568 KB |
Output is correct |
70 |
Correct |
414 ms |
39648 KB |
Output is correct |