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>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
typedef long long int ll;
using namespace std;
const int N = 200010;
int n,k,sz[N],dead[N],ans;
vector<pii> g[N];
void get_sz(int cur,int prev){
sz[cur] = 1;
for(auto &x : g[cur]) if(!dead[x.first] && x.first!=prev){
get_sz(x.first,cur);
sz[cur] += sz[x.first];
}
}
int dfs(int cur,int prev,int tot){
for(auto &x : g[cur]) if(!dead[x.first] && x.first!=prev){
if(sz[x.first]>(tot/2)) return dfs(x.first,cur,tot);
}
return cur;
}
int one(int start){
get_sz(start,-1);
return dfs(start,-1,sz[start]);
}
void rec(int cur){
int c = one(cur);
dead[c] = 1;
for(auto &x : g[c]) if(!dead[x.first]) rec(x.first);
map<int,int> cnt;
function<void (int,int,int)> dfs3 = [&](int cu,int st,int _depth){
if(_depth>k) return;
cnt[_depth] = cnt[_depth]?min(cnt[_depth],st):st;
for(auto &x : g[cu]) if(!dead[x.first])
dfs3(x.first,st+1,_depth+x.second);
};
function<void (int,int,int)> dfs4 = [&](int cu,int st,int _depth){
if(_depth>k) return;
if(cnt[k-_depth]) ans = min(ans,st+cnt[k-_depth]);
for(auto &x : g[cu]) if(!dead[x.first])
dfs4(x.first,st+1,_depth+x.second);
};
dfs3(c,0,0);
if(cnt[k]) ans = min(ans,cnt[k]); cnt.clear();
for(auto &x : g[c]) if(!dead[x.first]){
dfs4(x.first,1,x.second);
dfs3(x.first,1,x.second);
}
dead[c] = 0;
}
int best_path(int _n,int _k,int h[][2],int l[]){
ans = 1e9;
n = _n; k = _k;
for(int i=0;i<n-1;i++){
int a = h[i][0];
int b = h[i][1];
int c = l[i];
g[a].pb(mp(b,c));
g[b].pb(mp(a,c));
}
rec(0);
if(ans>=1e9) ans = -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void rec(int)':
race.cpp:45:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(cnt[k]) ans = min(ans,cnt[k]); cnt.clear();
^~
race.cpp:45:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(cnt[k]) ans = min(ans,cnt[k]); cnt.clear();
^~~
# | 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... |