이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
#define MAXN 200010
#define MAXK 1000010
using namespace std;
vector<pair<int,int> > G[MAXN];
bool check[MAXN];
int subtree[MAXN],k,res=INT_MAX,ans[MAXK];
void DFS_Subtree(int i,int p)
{
subtree[i]=1;
for (auto next : G[i])
{
if (next.first!=p && !check[next.first])
{
DFS_Subtree(next.first,i);
subtree[i]+=subtree[next.first];
}
}
}
int DFS_Centroid(int i,int p,int root)
{
for (auto next : G[i])
{
if (next.first!=p && !check[next.first] && subtree[next.first]>subtree[root]/2)
return DFS_Centroid(next.first,i,root);
}
return i;
}
void DFS_Paths(int i,int p,int d,int cnt)
{
if (d>k)
return;
if (ans[k-d] || k==d)
res=min(res,ans[k-d]+cnt);
for (auto next : G[i])
{
if (next.first!=p && !check[next.first])
DFS_Paths(next.first,i,d+next.second,cnt+1);
}
}
vector<int> v;
void DFS_Fill(int i,int p,int d,int cnt)
{
if (d>k)
return;
ans[d]=(!ans[d] && d)?cnt : min(ans[d],cnt);
v.push_back(d);
for (auto next : G[i])
{
if (next.first!=p && !check[next.first])
DFS_Fill(next.first,i,d+next.second,cnt+1);
}
}
void Solve(int i)
{
DFS_Subtree(i,i);
int c=DFS_Centroid(i,i,i);
for (auto next : G[c])
{
DFS_Paths(next.first,c,next.second,1);
DFS_Fill(next.first,c,next.second,1);
}
for (auto x : v)
ans[x]=0;
v.clear();
check[c]=true;
for (auto next : G[c])
{
if (!check[next.first])
Solve(next.first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for (int i=0;i<N-1;i++)
{
G[H[i][0]].push_back({H[i][1],L[i]});
G[H[i][1]].push_back({H[i][0],L[i]});
}
Solve(0);
return (res==INT_MAX?-1 : res);
}
# | 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... |