#include "race.h"
#include <vector>
using namespace std;
// frozen
int i,n,k;
int answer = -1;
vector<pair<int,int> > edge[200002];
bool check[200002];
int half_sum[1000002];
int find_middle(int x){
return x;
}
void dfs(int v,int sum,int cnt){
int i,sizes;
int nextv,cost;
check[v] = true;
if(half_sum[sum] == -1) half_sum[sum] = cnt;
else if(half_sum[sum] > cnt) half_sum[sum] = 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 get(int v){
int i;
for(i=0; i<=k; i++) half_sum[i] = -1;
dfs(v,0,0);
}
void race(int x){
int i,sizes;
int nextv;
int v = find_middle(x);
get(v);
for(i=0; i<=k/2; i++){
if(half_sum[i] != -1 && half_sum[k-i] != -1){
if(answer == -1) answer = half_sum[i]+half_sum[k-i];
else if(answer > half_sum[i]+half_sum[k-i]) answer = half_sum[i]+half_sum[k-i];
}
}
check[v] = true;
sizes = edge[v].size();
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);
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |