# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1151016 | Shadow1 | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
// #include "race.h"
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 500000
using ll = long long;
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << '\n';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
// #define int long long
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static signed solution;
inline
void my_assert(int e) {if (!e) abort();}
const int maxn = 2e5 + 5;
map<int, int> mp[maxn]; // mp[u][x] is length of minimum path with u as root and sum = x. (sum, min edges)
vector<pii> adj[maxn];
int k;
void dfs(int u, int p) {
for(auto &i : adj[u]) {
int w = i.sec, v = i.fir;
if(v == p) continue;
dfs(v, u);
mp[u][w] = 1;
if(mp[v][k-w] > 0)
mp[u][k] = mp[v][k-w] + 1;
// if(sz(mp[u]) < sz(mp[v])) swap(mp[u], mp[v]);
for(auto &x : mp[v]) {
if(mp[u][x.fir] > 0)
mp[u][x.fir] = min(mp[u][x.fir], x.sec);
else
mp[u][x.fir] = x.sec;
if(mp[u][k-(w+x.fir)] > 0)
mp[u][k] = min(mp[u][k], mp[u][k-(w+x.fir)] + x.sec + 1);
}
}
mp[u][0] = 0;
}
signed best_path(int N, int K, int H[][2], int L[])
{
int n = N;
k = K;
for(int i=0; i<n-1; ++i) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
dfs(0, -1);
int ans = maxn;
show(mp[6][5]);
for(int i=0; i<n; ++i) {
if(mp[i][k] > 0)
ans = min(ans, mp[i][k]);
}
return (ans == maxn ? -1 : ans);
}
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));
}
signed 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;
}