#include "race.h"
#include <vector>
#include <algorithm>
using namespace std;
// frozen
int i,n,k;
int answer = 999999999;
vector<pair<int,int> > edge[200002];
bool check[200002];
int half_sum[1000002];
int sum1,sum2;
int tsum;
int find_middle(int x){
return x;
}
void dfs(int v,long long sum,int cnt){
int i,sizes;
int nextv,cost;
if(sum > k) return;
check[v] = true;
if(half_sum[sum] > cnt) half_sum[sum] = cnt;
if(k%2 == 0 && sum == k/2 && tsum > cnt) tsum = cnt;
sizes = edge[v].size();
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
cost = edge[v][i].second;
if(check[nextv]) continue;
dfs(nextv,sum+cost,cnt+1);
}
check[v] = false;
}
void race(int x){
int i,sizes;
int nextv,t;
int v = find_middle(x);
if(k%2 == 0) sum1 = sum2 = 999999999;
check[v] = true;
sizes = edge[v].size();
for(i=0; i<=k; i++) half_sum[i] = 999999999;
half_sum[0] = 0;
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
tsum = 999999999;
dfs(nextv,edge[v][i].second,1);
if(tsum == 999999999) continue;
if(sum1 == 999999999){
sum1 = tsum;
}else if(sum2 == 999999999){
sum2 = tsum;
if(sum1 > sum2) swap(sum1,sum2);
}else if(!(sum2 < tsum)){
sum2 = tsum;
if(sum1 > sum2) swap(sum1,sum2);
}
}
if(k%2 ==1) t = k/2;
else t = (k/2)-1;
for(i=0; i<=t; i++) if(half_sum[i] != 999999999 && half_sum[k-i] != 999999999 && answer > half_sum[i]+half_sum[k-i]) answer = half_sum[i]+half_sum[k-i];
if(k%2 == 0 && sum1 != 999999999 && sum2 != 999999999 && answer > sum1+sum2) answer = sum1+sum2;
for(i=0; i<sizes; i++){
nextv = edge[v][i].first;
if(!check[nextv]) race(nextv);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int i;
n = N; k = K;
for(i=0; i<n-1; i++){
edge[H[i][0]].push_back(make_pair(H[i][1],L[i]));
edge[H[i][1]].push_back(make_pair(H[i][0],L[i]));
}
race(0);
if(answer == 999999999) answer = -1;
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |