이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
const int N=2e5+5;
int res,k;
map<int,int>mp[N];
vector<pii>adj[N];
void dfs(int u,int p=-1,int h=0,int w=0)
{
mp[u][w]=h;
int tmp=u;
for(auto&to:adj[u])
{
int v=to.fi;
if(v==p) continue;
dfs(v,tmp,h+1,w+to.se);
if(mp[u].size()<mp[v].size()) swap(u,v);
for(auto&x:mp[v])
{
if(mp[u].count(k+2*w-x.fi))
{
res=min(res,x.se+mp[u][k+2*w-x.fi]-2*h);
}
}
for(auto&x:mp[v])
{
if(!mp[u].count(x.fi)||mp[u][x.fi]>x.se) mp[u][x.fi]=x.se;
}
}
}
int best_path(int n,int nk,int h[][2],int l[])
{
k=nk;
for(int i=0;i<n-1;i++)
{
adj[h[i][0]].push_back(make_pair(h[i][1],l[i]));
adj[h[i][1]].push_back(make_pair(h[i][0],l[i]));
}
res=1e9;
dfs(0);
if(res==1e9) res=-1;
return res;
}
/*
int main()
{
int n,k;
cin>>n>>k;
int h[n][2],l[n];
for(int i=0;i<n-1;i++) cin>>h[i][0]>>h[i][1];
for(int i=0;i<n-1;i++) cin>>l[i];
cout<<best_path(n,k,h,l);
}
4 3
0 1
1 2
1 3
1 2 4
*/
# | 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... |