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 "race.h"
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxN=2e5+100;
int k;
int n;
vector<pii> v[mxN];
ll ans=1e9;
ll sz[mxN];
ll d1[mxN];//using weight of the edge
ll d2[mxN];//jut dist (weight=1)
void dfs(int node,int par,ll d=0){
d2[node]=d2[par]+1;
d1[node]=d;
sz[node]=1;
for(auto x:v[node]){
if(x.F==par) continue;
dfs(x.F,node,d+x.S);
sz[node]+=sz[x.F];
}
}
map<ll,ll> mp[mxN];
void dfs2(int node,int par){
int bc=-1;
ll bs=0;
for(auto x:v[node]){
if(x.F!=par&&sz[x.F]>bs)
bs=sz[x.F],bc=x.F;
if(x.F!=par){
dfs2(x.F,node);
}
}
if(bc!=-1){
swap(mp[node],mp[bc]);
}
mp[node][d1[node]]=d2[node];
if(mp[node].count(d1[node]+k)>0){
// // dbg(node,-d2[node],mp[node][d1[node]+k])
ans=min(ans,-d2[node]+mp[node][d1[node]+k]);
}
for(auto x:v[node]){
if(x.F==par||x.F==bc) continue;
for(auto it:mp[x.F]){
ll z=k-it.F+2*d1[node];
if(mp[node].count(z)>0){
// dbg(node,x.F,t)
ans=min(ans,mp[node][z]+it.S);
}
}
for(auto it:mp[x.F]){
if(mp[node].count(it.F)>0)
mp[node][it.first]=min(mp[node][it.first],
it.second);
else mp[node][it.F]=it.S;
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
for(int i=0;i+1<n;i++){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
dfs(0,0);
dfs2(0,0);
if(ans==1e9) return -1;
return ans;
}
#ifdef IOI
// #include "race.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}
int main()
{
int ans;
read_input();
ans = best_path(N,K,H,L);
if(ans==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
return 0;
}
#endif
# | 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... |