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 <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <iomanip>
#include <cmath>
#include <limits>
#include <map>
#include <utility>
#include <cctype>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <functional>
#include <iterator>
using namespace std;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
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__);
#define endl "\n"
#define pb push_back
#define F first
#define S second
#define ll long long
#define mod 1000000007
#define all(v) v.begin(),v.end()
const int MAXN=2e5+1;
const int MAXT=101;
const int INF=1e9;
vector<pair<int,int>> v[MAXN];
int n,k;
map<int,int> mp[MAXN];
int ans=INF;
int dfs(int node,int par){
if(v[node].size()==1&&v[node][0].F==par) return -1;
mp[node][0]=0;
for(auto x:v[node]){
if(x.F==par) continue;
int a=dfs(x.F,node);
if(x.S>k) continue;
if(a==-1){
int target=k-x.S;
if(target>=0&&mp[node].count(target)){
ans=min(ans,mp[node][target]+1);
}
mp[node][x.S]=1;
}else{
for(auto [key,value]:mp[x.F]){
if(key==0) continue;
int target=k-(key+x.S);
if(target<0) continue;
if(mp[node].count(target)){
ans=min(ans,mp[node][target]+value+1);
}
}
for(auto [key,val]:mp[x.F]){
if(key+x.S>MAXT) continue;
if(mp[node].count(key+x.S))
mp[node][key+x.S]=min(mp[node][key+x.S],val+1);
else mp[node][key+x.S]=val+1;
}
}
}
return 1;
}
int best_path(int N, int K, int H[][2], int L[])
{
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];
v[a].pb({b,c});
v[b].pb({a,c});
}
dfs(0,0);
if(ans==INF)
return -1;
return ans;
}
#ifdef IOI
#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... |