//#include <race.h>
#include <bits/stdc++.h>
using namespace std;
map<int,long long> mp[200005] ;
vector<pair<int,int>> adj[200005];
int target = 0,sum[200005],height[200005],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( nod == 6 && u.first == 8 )
{
cout << real << '\n';
}*/
if ( mp[nod].find(real) != mp[nod].end())
{
//cout << nod << " " << u.first << " " << k.first << " " << k.second << '\n';
ans = min ( long(ans), long(k.second + mp[nod][real] - 2 * height[nod]));
}
}
for ( auto k : mp[u.first])
{
if( mp[nod][k.first] == 0 )
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 costs [] )
{
if( k == 1 )
{
return 0 ;
}
target =k ;
for ( int i = 1; i < n ; i ++ )
{
adj[h[i][1]].push_back({h[i][0],costs[i]});
adj[h[i][0]].push_back({h[i][1],costs[i]});
}
precomp(0,-1,1);
small(0,-1);
return ans == (long long)1e9 ? -1: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 |
Incorrect |
2 ms |
19548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
19548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
19548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
19548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |