이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <bits/stdc++.h>
#define LL long long
#define ii pair<LL,LL>
#define F first
#define S second
using namespace std;
const LL NN=2e5+100;
vector<ii> g[NN];
LL dp[NN][111];
LL n,k;
int ans=1e9;
multiset<int> st;
void dfs(LL u,LL p)
{
dp[u][0]=0;
for(auto x:g[u]){
if(x.F==p)continue;
dfs(x.F,u);
}
for(auto x:g[u]){
if(x.F==p)continue;
for(LL i=0;i<=k;i++){
if(i-x.S>=0)dp[u][i]=min(dp[x.F][i-x.S]+1,dp[u][i]);
}
}
if((g[u].size()==2&&u!=p)||g[u].size()==1)return;
for(LL i=0;i<=k;i++){
st.clear();
for(auto x:g[u]){
if(x.F==p)continue;
if(i-x.S>=0)st.insert(dp[x.F][i-x.S]);
}
for(auto x:g[u]){
if(x.F==p)continue;
if(i-x.S>=0)st.erase(st.find(dp[x.F][i-x.S]));
LL j=(k-i)-x.S;
if(j>=0){
LL ret=*st.begin();
ret+=dp[x.F][j];
ans=min(ans,(int)ret+2);
}
if(i-x.S>=0)st.insert(dp[x.F][i-x.S]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
ans=1e9;
n=N;
k=K;
for(int i=0;i<n;i++){
for(int j=0;j<=k;j++){
dp[i][j]=1e9;
}
}
for(int i=0;i<n-1;i++){
int u=H[i][0];
int v=H[i][1];
int w=L[i];
g[u].push_back({v,w});
g[v].push_back({u,w});
}
dfs(0,0);
for(int i=0;i<n;i++){
ans=min(ans,(int)dp[i][k]);
}
if(ans==1e9)ans=-1;
return ans;
}
| # | 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... |