This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include<vector>
#define rep(i, n) for(int i=0; i<n; i++)
#define pii pair<int, ll>
#define ll long long
using namespace std;
ll TARGET;
const int INF = 1e9;
int BEST;
vector<vector<pii>> graph;
void DFS(int p, int node, ll w, int lenght){
if(w > TARGET) return;
if(w == TARGET) BEST = min(BEST,lenght);
for(auto &e: graph[node])
{
if(e.first == p) continue;
DFS(node, e.first, w + e.second, lenght + 1);
}
}
int best_path(int N, int K, int H[][2], int L[]){
graph.assign(N,vector<pii>{});
TARGET = K;
int a, b, l;
for(int i=0; i<N-1; i++)
{
a = H[i][0]; b = H[i][1]; l = L[i];
graph[a].push_back({b,l});
graph[b].push_back({a,l});
}
BEST = INF;
for(int i=0; i<N-1; i++) DFS(-1,i,0,0);
if(BEST == INF) return -1;
return BEST;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |