#include <bits/stdc++.h>
#include "race.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define ld long double
#define en exit(0);
#define pb push_back
#define fi first
#define se second
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 5;
const int K = 105;
const int INF = 1e9 + 5;
vector<pair<int,int> > g[N];
int dp[N][K]; // dp[u][l] - min number of highways with length = l, ending at u in this subtree
int k, res = INF;
void dfs(int u, int p)
{
for(int i = 0;i <= k;i++)
dp[u][i] = INF;
dp[u][0] = 0;
for(auto [v, w] : g[u])
{
if(v != p)
{
dfs(v, u);
for(int j = 0;k - j - w >= 0;j++)
res = min(res, dp[u][j] + 1 + dp[v][k - j - w]);
for(int j = w;j <= k;j++)
dp[u][j] = min(dp[u][j], dp[v][j - w] + 1);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
for(int i = 0;i < N - 1;i++)
{
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1, 1);
return (res == INF ? -1 : res);
}
# | 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... |