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>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second
struct triple{
int a,b,c;
};
int const n=2e5;
int const k_=1e2+1;
int dp[n][k_];
vpi adj[n];
int const INF=1e9;
int mn=INF;
int k;
void dfs(int now, int par)
{
vector<triple> cur(k+1);
F0R(i,k+1)
{
cur[i]={INF,-1,-1};
}
cur[0].a=0;
cur[0].b=now;
trav(chi,adj[now])
{
if(chi.fi==par)continue;
dfs(chi.fi,now);
F0R(i,k+1)
{
if(i-chi.se<0)continue;
if(dp[chi.fi][i-chi.se]+1 == cur[i].a)
{
cur[i].c=chi.fi;
}
else if(dp[chi.fi][i-chi.se]+1 < cur[i].a)
{
cur[i] = {dp[chi.fi][i-chi.se]+1, chi.fi,-1};
}
}
}
F0R(i,k+1)
{
if(cur[i].a==-1)continue;
if(cur[k-i].a==-1)continue;
if(cur[i].b==cur[k-i].b)
{
if(cur[i].c==-1&&cur[k-i].c==-1)continue;
}
mn=min(mn,cur[i].a+cur[k-i].a);
}
F0R(i,k+1)
{
dp[now][i]=cur[i].a;
}
}
int best_path(int N, int K, int H[][2], int L[]){
k=K;
F0R(i,N-1)
{
int u = H[i][0];
int v = H[i][1];
adj[u].pb({v,L[i]});
adj[v].pb({u,L[i]});
}
dfs(0,-1);
if(mn>=INF)return -1;
return mn;
}
# | 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... |