#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>>
const int mod = 1e9+7;
const int INF = 1e9;
int n,k,ans=INF;
vpii adj[MAX];
vi dead(MAX),sz(MAX),mpp(1e6+5);
void dfs(int node,int par){
sz[node]=1;
for(auto u : adj[node]){
if(dead[u.f] || u.f==par) continue;
sz[node]+=sz[u.f];
}
}
int findc(int node,int par,int val){
for(auto u : adj[node]){
if(dead[u.f] || u.f==par) continue;
if(sz[u.f]>=val/2) return findc(u.f,node,sz[u.f]); // val olmalidir
}
return node;
}
void check(int node,int par,int len,int sayi){
if(len>k) return;
if(mpp[k-len]!=INF) ans=min(ans,mpp[k-len]+sayi);
for(auto u : adj[node]){
if(dead[u.f] || u.f==par) continue;
check(u.f,node,len+u.s,sayi+1);
}
}
void build(int node,int par,int len,int sayi){
if(len>k) return;
mpp[len]=min(mpp[len],sayi);
for(auto u : adj[node]){
if(dead[u.f] || u.f==par) continue;
check(u.f,node,len+u.s,sayi+1);
}
}
void solve(int node){
dfs(node,node);
int x = findc(x,x,sz[x]);
dead[x]=true;
for(auto u : adj[x]){
if(dead[u.f]) continue;
check(u.f,x,u.s,1);
build(u.f,x,u.s,1);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N,k=K;
for(int i=0;i<n-1;i++){
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
fill(mpp.begin(),mpp.end(),INF);
int cev=n+2;
solve(0);
if(cev==n+2) cev=-1;
return cev;
}
Compilation message
race.cpp: In function 'void solve(int)':
race.cpp:58:25: warning: 'x' is used uninitialized in this function [-Wuninitialized]
58 | int x = findc(x,x,sz[x]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |