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 "race.h"
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
int n, k;
vector<pii> g[200010];
bool v[200010];
int mn[200010][101];
int res=1e9;
void dfs(int s){
v[s]=true;
mn[s][0]=1;
for(auto pr:g[s]){
int u=pr.ft, d=pr.sc;
if(!v[u]){
dfs(u);
for(int sum=0; ( (sum+d)<=k ); sum++){
if( (mn[u][sum]>0) ){
if(mn[s][sum+d]==0){
mn[s][sum+d]=mn[u][sum]+1;
}
else{
mn[s][sum+d]=min(mn[s][sum+d], mn[u][sum]+1);
}
}
}
}
}
for(int sum=0; sum<=k; sum++){
if( (mn[s][sum]>0) && (mn[s][k-sum]>0) ){
res=min(res, mn[s][sum]+mn[s][k-sum]-1);
}
}
return;
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
for(int i=0; i<n; i++){
int u=H[i][0]+1, v=H[i][1]+1, d=L[i];
g[u].pb({v, d}); g[v].pb({u, d});
}
dfs(1);
if(res==(1e9)){
return -1;
}
return res-1;
}
# | 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... |