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 <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#define pi 3.141592653589793238
#define ll long long
#define ld long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 998244353ll
#define INF 1000000000000000000
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast \
ios_base::sync_with_stdio (false); \
cin.tie (NULL); \
cout.tie (NULL)
using namespace std;
vector <ll > minlen(1000000+1,mod);
vector <ll > minlenothers(1000000+1,mod);
vector <ll > ch;
void setup(ll v, ll p, ll width[], vector <pair<ll ,ll >> adj[])
{
width[v]=1;
for(auto u:adj[v])
if(u.ff!=p)
{
setup(u.ff,v,width,adj);
width[v]+=width[u.ff];
}
}
ll traverse(ll n, ll v, ll p, ll width[],vector <pair<ll ,ll >> adj[])
{
ll outlier=-1;
for(auto u:adj[v])
if(u.ff!=p)
if(width[u.ff]>n/2)
{
outlier=u.ff;
break;
}
if(outlier==-1)
return v;
else
return traverse(n,outlier,v,width,adj);
}
void dfs(ll v, ll p, ll k, ll &ans, vector <ll > &mydist, ll depth,vector <pair<ll ,ll >> adj[])
{
if(mydist[v]<=k)
{
ll d=mydist[v];
minlen[d]=min(minlen[d],depth);
if(d==k)
ans=min(ans,minlen[d]);
ans=min(ans,depth+minlenothers[k-d]);
ch.push_back(d);
}
for(auto u:adj[v])
if(u.ff!=p)
{
mydist[u.ff]=mydist[v]+u.ss;
dfs(u.ff,v,k,ans,mydist, depth+1,adj);
}
}
void find_children(ll v, ll p, vector <ll > &ch,vector <pair<ll ,ll >> adj[])
{
ch.push_back(v);
for(auto u:adj[v])
if(u.ff!=p)
find_children(u.ff,v,ch,adj);
}
ll solve(ll n, ll k, vector <pair<ll ,ll >> adj[])
{
if(n==1)
return mod;
ll width[n+1];
setup(1,-1,width,adj);
ll v=traverse(n, 1,-1,width,adj);
vector <ll > mydist(n+1,mod);
ll ans=mod;
ch.clear();
vector <ll > revert;
for(auto u:adj[v])
{
mydist[u.ff]=u.ss;
dfs(u.ff,v,k,ans,mydist,1,adj);
for(auto x:ch)
{
minlenothers[x]=min(minlenothers[x],minlen[x]);
minlen[x]=mod;
revert.pb(x);
}
ch.clear();
}
for(auto x:revert)
minlenothers[x]=mod;
for(auto u:adj[v])
{
find_children(u.ff,v,ch,adj);
sort(all(ch));
ll s=sz(ch);
vector <ll > m(n+1);
rep(i,0,s-1)
m[ch[i]]=i+1;
vector <pair<ll ,ll >> nadj[s+1];
rep(i,0,s-1)
{
ll vt=ch[i];
for(auto u:adj[vt])
if(u.ff!=v)
{
nadj[m[vt]].pb({m[u.ff],u.ss});
}
}
ch.clear();
ans=min(ans,solve(s,k,nadj));
}
return ans;
}
int best_path(int n, int k, int h[][2], int l[])
{
vector <pair<ll ,ll >> adj[n+1];
rep(i,0,n-2)
{
adj[h[i][0]+1].pb({h[i][1]+1,l[i]});
adj[h[i][1]+1].pb({h[i][0]+1,l[i]});
}
ll x=solve(n,k,adj);
return (x==mod?-1:x);
}
// signed main()
// {
// fast;
// freopen("milkorder.in","r",stdin);
// // freopen("milkorder.out","w",stdout);
// ll n,k;
// cin>>n>>k;
// int h[n-1][2];
// int l[n-1];
// rep(i,0,n-2)
// {
// cin>>h[i][0]>>h[i][1]>>l[i];
// }
// cout<<best_path(n,k,h,l)<<endl;
// return 0;
// }
# | 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... |