이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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
int const n=2e5+3;
int const k_=1e2+3;
pair<int,pii> dp[n][k_];
vpi adj[n];
int const INF=1e9;
int mn=INF;
int k;
void dfs(int now, int par)
{
F0R(i,k+1)
{
dp[now][i]={INF,pii{-1,-1}};
}
dp[now][0].fi=0;
dp[now][0].se.fi=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].fi+1 == dp[now][i].fi)
{
dp[now][i].se.se=chi.fi;
}
else if(dp[chi.fi][i-chi.se].fi+1 < dp[now][i].fi)
{
dp[now][i] = {dp[chi.fi][i-chi.se].fi+1, pii{chi.fi,-1}};
}
}
}
F0R(i,k+1)
{
if(dp[now][i].se.fi==-1)continue;
if(dp[now][k-i].se.fi==-1)continue;
if(dp[now][i].se.fi==dp[now][k-i].se.fi)
{
if(dp[now][i].se.se==-1&&dp[now][k-i].se.se==-1)continue;
}
mn=min(mn,dp[now][i].fi+dp[now][k-i].fi);
}
}
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... |