#include <bits/stdc++.h>
using namespace std;
map<int,long long> mp[300005] ;
vector<pair<int,long long>> adj[300005];
long long target = 0,sum[300005],height[300005],ans = 1e9,n,k;
void precomp(int nod, int p, int hd)
{
height[nod] = hd ;
for ( auto u : adj[nod] )
{
if ( u.first != p )
{
sum[u.first] += sum[nod] + u.second;
precomp(u.first,nod,hd + 1);
}
}
}
void small(int nod,int p )
{
mp[nod][sum[nod]] = height[nod];
long long real_target = target + 2 * sum[nod];
for ( auto u : adj[nod] )
{
if ( u.first != p )
{
small(u.first,nod);
}
}
for ( auto u : adj[nod] )
{
if( u.first != p )
{
if( mp[u.first].size() > mp[nod].size())
swap(mp[nod],mp[u.first]);
for ( auto k : mp[u.first] )
{
long long real = real_target - k.first ;
if ( mp[nod].find(real) != mp[nod].end())
{
//cout << nod << " " << u.first << " " << k.first << " " << k.second << '\n';
ans = min ( ans, k.second + mp[nod][real] - 2 * height[nod]);
}
}
for ( auto k : mp[u.first])
{
if( mp[nod].find(k.first) == mp[nod].end() )
mp[nod][k.first] = k .second;
else
mp[nod][k.first] = min(mp[nod][k.first],k.second);
}
}
}
/*if( nod == 6)
{
cout << mp[nod][real_target]<< '\n';
}*/
}
int best_path(int N, int K, int H[][2], int L [] )
{
if( K == 1 )
{
return 0 ;
}
target =K ;
for ( int i = 0; i < N - 1 ; i ++ )
{
adj[H[i][1]].push_back({H[i][0],L[i]});
adj[H[i][0]].push_back({H[i][1],L[i]});
}
precomp(0,-1,1);
small(0,-1);
if ( ans == 1e9 )
return -1;
else
return ans;
}
//int v[200005];
/*int main()
{
cin >> n >> k;
for ( int i = 1; i < n ; i ++ )
{
cin >> h[i][0] >> h[i][1] >> v[i] ;
}
cout << best_path(n,k,h,v);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
3 ms |
25692 KB |
Output is correct |
3 |
Correct |
3 ms |
25692 KB |
Output is correct |
4 |
Correct |
3 ms |
25692 KB |
Output is correct |
5 |
Correct |
3 ms |
25688 KB |
Output is correct |
6 |
Correct |
3 ms |
25692 KB |
Output is correct |
7 |
Correct |
4 ms |
25692 KB |
Output is correct |
8 |
Correct |
4 ms |
25688 KB |
Output is correct |
9 |
Correct |
3 ms |
25692 KB |
Output is correct |
10 |
Correct |
3 ms |
25692 KB |
Output is correct |
11 |
Correct |
3 ms |
25944 KB |
Output is correct |
12 |
Correct |
3 ms |
25692 KB |
Output is correct |
13 |
Correct |
3 ms |
25692 KB |
Output is correct |
14 |
Correct |
3 ms |
25692 KB |
Output is correct |
15 |
Correct |
3 ms |
25692 KB |
Output is correct |
16 |
Correct |
3 ms |
25692 KB |
Output is correct |
17 |
Correct |
3 ms |
25692 KB |
Output is correct |
18 |
Correct |
3 ms |
25692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
3 ms |
25692 KB |
Output is correct |
3 |
Correct |
3 ms |
25692 KB |
Output is correct |
4 |
Correct |
3 ms |
25692 KB |
Output is correct |
5 |
Correct |
3 ms |
25688 KB |
Output is correct |
6 |
Correct |
3 ms |
25692 KB |
Output is correct |
7 |
Correct |
4 ms |
25692 KB |
Output is correct |
8 |
Correct |
4 ms |
25688 KB |
Output is correct |
9 |
Correct |
3 ms |
25692 KB |
Output is correct |
10 |
Correct |
3 ms |
25692 KB |
Output is correct |
11 |
Correct |
3 ms |
25944 KB |
Output is correct |
12 |
Correct |
3 ms |
25692 KB |
Output is correct |
13 |
Correct |
3 ms |
25692 KB |
Output is correct |
14 |
Correct |
3 ms |
25692 KB |
Output is correct |
15 |
Correct |
3 ms |
25692 KB |
Output is correct |
16 |
Correct |
3 ms |
25692 KB |
Output is correct |
17 |
Correct |
3 ms |
25692 KB |
Output is correct |
18 |
Correct |
3 ms |
25692 KB |
Output is correct |
19 |
Correct |
4 ms |
25692 KB |
Output is correct |
20 |
Correct |
3 ms |
25780 KB |
Output is correct |
21 |
Correct |
4 ms |
26060 KB |
Output is correct |
22 |
Correct |
4 ms |
25948 KB |
Output is correct |
23 |
Correct |
4 ms |
25948 KB |
Output is correct |
24 |
Correct |
3 ms |
25948 KB |
Output is correct |
25 |
Correct |
4 ms |
25948 KB |
Output is correct |
26 |
Correct |
4 ms |
25948 KB |
Output is correct |
27 |
Correct |
3 ms |
25948 KB |
Output is correct |
28 |
Correct |
4 ms |
25948 KB |
Output is correct |
29 |
Correct |
4 ms |
26072 KB |
Output is correct |
30 |
Correct |
4 ms |
25948 KB |
Output is correct |
31 |
Correct |
3 ms |
25948 KB |
Output is correct |
32 |
Correct |
4 ms |
25948 KB |
Output is correct |
33 |
Correct |
4 ms |
25948 KB |
Output is correct |
34 |
Correct |
4 ms |
26072 KB |
Output is correct |
35 |
Correct |
4 ms |
25948 KB |
Output is correct |
36 |
Correct |
3 ms |
25948 KB |
Output is correct |
37 |
Correct |
3 ms |
25948 KB |
Output is correct |
38 |
Correct |
3 ms |
26044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
3 ms |
25692 KB |
Output is correct |
3 |
Correct |
3 ms |
25692 KB |
Output is correct |
4 |
Correct |
3 ms |
25692 KB |
Output is correct |
5 |
Correct |
3 ms |
25688 KB |
Output is correct |
6 |
Correct |
3 ms |
25692 KB |
Output is correct |
7 |
Correct |
4 ms |
25692 KB |
Output is correct |
8 |
Correct |
4 ms |
25688 KB |
Output is correct |
9 |
Correct |
3 ms |
25692 KB |
Output is correct |
10 |
Correct |
3 ms |
25692 KB |
Output is correct |
11 |
Correct |
3 ms |
25944 KB |
Output is correct |
12 |
Correct |
3 ms |
25692 KB |
Output is correct |
13 |
Correct |
3 ms |
25692 KB |
Output is correct |
14 |
Correct |
3 ms |
25692 KB |
Output is correct |
15 |
Correct |
3 ms |
25692 KB |
Output is correct |
16 |
Correct |
3 ms |
25692 KB |
Output is correct |
17 |
Correct |
3 ms |
25692 KB |
Output is correct |
18 |
Correct |
3 ms |
25692 KB |
Output is correct |
19 |
Correct |
80 ms |
52636 KB |
Output is correct |
20 |
Correct |
74 ms |
52396 KB |
Output is correct |
21 |
Correct |
76 ms |
52120 KB |
Output is correct |
22 |
Correct |
70 ms |
51796 KB |
Output is correct |
23 |
Correct |
103 ms |
64716 KB |
Output is correct |
24 |
Correct |
75 ms |
54868 KB |
Output is correct |
25 |
Correct |
63 ms |
51792 KB |
Output is correct |
26 |
Correct |
44 ms |
58360 KB |
Output is correct |
27 |
Correct |
107 ms |
64080 KB |
Output is correct |
28 |
Correct |
205 ms |
99920 KB |
Output is correct |
29 |
Correct |
181 ms |
98620 KB |
Output is correct |
30 |
Correct |
106 ms |
64084 KB |
Output is correct |
31 |
Correct |
110 ms |
64084 KB |
Output is correct |
32 |
Correct |
134 ms |
64080 KB |
Output is correct |
33 |
Correct |
155 ms |
68948 KB |
Output is correct |
34 |
Correct |
207 ms |
101740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
3 ms |
25692 KB |
Output is correct |
3 |
Correct |
3 ms |
25692 KB |
Output is correct |
4 |
Correct |
3 ms |
25692 KB |
Output is correct |
5 |
Correct |
3 ms |
25688 KB |
Output is correct |
6 |
Correct |
3 ms |
25692 KB |
Output is correct |
7 |
Correct |
4 ms |
25692 KB |
Output is correct |
8 |
Correct |
4 ms |
25688 KB |
Output is correct |
9 |
Correct |
3 ms |
25692 KB |
Output is correct |
10 |
Correct |
3 ms |
25692 KB |
Output is correct |
11 |
Correct |
3 ms |
25944 KB |
Output is correct |
12 |
Correct |
3 ms |
25692 KB |
Output is correct |
13 |
Correct |
3 ms |
25692 KB |
Output is correct |
14 |
Correct |
3 ms |
25692 KB |
Output is correct |
15 |
Correct |
3 ms |
25692 KB |
Output is correct |
16 |
Correct |
3 ms |
25692 KB |
Output is correct |
17 |
Correct |
3 ms |
25692 KB |
Output is correct |
18 |
Correct |
3 ms |
25692 KB |
Output is correct |
19 |
Correct |
4 ms |
25692 KB |
Output is correct |
20 |
Correct |
3 ms |
25780 KB |
Output is correct |
21 |
Correct |
4 ms |
26060 KB |
Output is correct |
22 |
Correct |
4 ms |
25948 KB |
Output is correct |
23 |
Correct |
4 ms |
25948 KB |
Output is correct |
24 |
Correct |
3 ms |
25948 KB |
Output is correct |
25 |
Correct |
4 ms |
25948 KB |
Output is correct |
26 |
Correct |
4 ms |
25948 KB |
Output is correct |
27 |
Correct |
3 ms |
25948 KB |
Output is correct |
28 |
Correct |
4 ms |
25948 KB |
Output is correct |
29 |
Correct |
4 ms |
26072 KB |
Output is correct |
30 |
Correct |
4 ms |
25948 KB |
Output is correct |
31 |
Correct |
3 ms |
25948 KB |
Output is correct |
32 |
Correct |
4 ms |
25948 KB |
Output is correct |
33 |
Correct |
4 ms |
25948 KB |
Output is correct |
34 |
Correct |
4 ms |
26072 KB |
Output is correct |
35 |
Correct |
4 ms |
25948 KB |
Output is correct |
36 |
Correct |
3 ms |
25948 KB |
Output is correct |
37 |
Correct |
3 ms |
25948 KB |
Output is correct |
38 |
Correct |
3 ms |
26044 KB |
Output is correct |
39 |
Correct |
80 ms |
52636 KB |
Output is correct |
40 |
Correct |
74 ms |
52396 KB |
Output is correct |
41 |
Correct |
76 ms |
52120 KB |
Output is correct |
42 |
Correct |
70 ms |
51796 KB |
Output is correct |
43 |
Correct |
103 ms |
64716 KB |
Output is correct |
44 |
Correct |
75 ms |
54868 KB |
Output is correct |
45 |
Correct |
63 ms |
51792 KB |
Output is correct |
46 |
Correct |
44 ms |
58360 KB |
Output is correct |
47 |
Correct |
107 ms |
64080 KB |
Output is correct |
48 |
Correct |
205 ms |
99920 KB |
Output is correct |
49 |
Correct |
181 ms |
98620 KB |
Output is correct |
50 |
Correct |
106 ms |
64084 KB |
Output is correct |
51 |
Correct |
110 ms |
64084 KB |
Output is correct |
52 |
Correct |
134 ms |
64080 KB |
Output is correct |
53 |
Correct |
155 ms |
68948 KB |
Output is correct |
54 |
Correct |
207 ms |
101740 KB |
Output is correct |
55 |
Correct |
12 ms |
29016 KB |
Output is correct |
56 |
Correct |
8 ms |
27812 KB |
Output is correct |
57 |
Correct |
44 ms |
50116 KB |
Output is correct |
58 |
Correct |
32 ms |
42444 KB |
Output is correct |
59 |
Correct |
59 ms |
64716 KB |
Output is correct |
60 |
Correct |
187 ms |
98896 KB |
Output is correct |
61 |
Correct |
153 ms |
67416 KB |
Output is correct |
62 |
Correct |
124 ms |
64136 KB |
Output is correct |
63 |
Correct |
166 ms |
64168 KB |
Output is correct |
64 |
Correct |
301 ms |
119124 KB |
Output is correct |
65 |
Correct |
277 ms |
117076 KB |
Output is correct |
66 |
Correct |
192 ms |
95572 KB |
Output is correct |
67 |
Correct |
95 ms |
56756 KB |
Output is correct |
68 |
Correct |
191 ms |
80464 KB |
Output is correct |
69 |
Correct |
224 ms |
85216 KB |
Output is correct |
70 |
Correct |
197 ms |
78312 KB |
Output is correct |