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;
const int maxN=200005;
const long long big=100000000000000;
long long k;
long long minans=INT_MAX;
vector<int>a[maxN];
map<pair<int,int>,int>dist;
map<long long,long long>path;
bool used[maxN];
int sz[maxN];
int upd_sz(int n,int parent=-1) {
sz[n]=1;
for(int i:a[n]) {
if(i!=parent && used[i]==0)sz[n]+=upd_sz(i,n);
}
return sz[n];
}
int find_cen(int n,int siz,int parent=-1) {
for(int i:a[n]) {
if(used[i]==0 && sz[i]>siz/2 && i!=parent)return find_cen(i,siz,n);
}
return n;
}
void upd_ans(int n,int depth,long long distance,int parent=-1) {
if(k-distance>=0)minans=min(minans,depth+path[k-distance]+big);
for(int i:a[n]) {
if(used[i]==0 && i!=parent) {
upd_ans(i,depth+1,distance+dist[{i,n}],n);
}
}
}
void upd_score(int n,int depth,long long distance,int parent=-1) {
path[distance]=min(path[distance],depth-big);
for(int i:a[n]) {
if(used[i]==0 && i!=parent) {
upd_score(i,depth+1,distance+dist[{i,n}],n);
}
}
}
void dfs(int n) {
int c=find_cen(n,upd_sz(n));
used[c]=1;
path.clear();
path[0]=-big;
for(int i:a[c]) {
if(used[i]==0) {
upd_ans(i,1,dist[{c,i}]);
upd_score(i,1,dist[{c,i}]);
}
}
for(int i:a[c]) {
if(used[i]==0)dfs(i);
}
}
int best_path(int N,int K,int H[][2],int L[]) {
k=K;
for(int i=0;i<N-1;i++) {
a[H[i][0]].push_back(H[i][1]);
a[H[i][1]].push_back(H[i][0]);
dist[{H[i][0],H[i][1]}]=dist[{H[i][1],H[i][0]}]=L[i];
}
dfs(0);
if(minans==INT_MAX)return -1;
else return minans;
}
/**
int main() {
int n;
cin>>n>>k;
for(int i=0;i<n-1;i++) {
int z;
int x,y;cin>>x>>y>>z;
a[x].push_back(y);
a[y].push_back(x);
dist[{x,y}]=dist[{y,x}]=z;
}
dfs(0);
if(minans==INT_MAX)cout<<-1<<endl;
else cout<<minans<<endl;
return 0;
}*/
/**
4 3
0 1 1
1 2 2
1 3 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... |