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 <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> adj[200001];
vector<vector<pair<int, int>>> x;
vector<vector<int>> dp;
int _N, _K, mn = INT_MAX;
void dfs(int v, int p = -1)
{
dp[v][0] = 0;
for (auto [i, w] : adj[v])
{
if (i != p)
dfs(i, v);
}
x.assign(_K + 1, {});
for (auto [i, w] : adj[v])
{
if (i == p)
continue;
for (int j = w; j <= _K; j++)
{
if (dp[i][j - w] == INT_MAX)
continue;
if (x[j].size() < 2)
x[j].push_back({dp[i][j - w] + 1, i});
else if (dp[i][j - w] + 1 < x[j][1].first)
x[j][1] = {dp[i][j - w] + 1, i};
if (x[j].size() == 2 && x[j][0].first > x[j][1].first)
swap(x[j][0], x[j][1]);
dp[v][j] = min(dp[v][j], dp[i][j - w] + 1);
}
}
if (!x[_K].empty())
mn = min(mn, x[_K][0].first);
for (int i = 1; i < _K; i++)
{
if (x[i].empty() || x[_K - i].empty())
continue;
if (x[i][0].second != x[_K - i][0].second)
mn = min(mn, x[i][0].first + x[_K - i][0].first);
else if (1 < (int)x[_K - i].size())
mn = min(mn, x[i][0].first + x[_K - i][1].first);
}
}
int best_path(int n, int k, int h[][2], int l[])
{
_N = n;
_K = k;
for (int i = 0; i < n - 1; i++)
{
adj[h[i][0] + 1].push_back({h[i][1] + 1, l[i]});
adj[h[i][1] + 1].push_back({h[i][0] + 1, l[i]});
}
dp.assign(n, vector<int>(k + 1, INT_MAX));
dfs(0);
if (mn == INT_MAX)
return -1;
else
return mn;
}
# | 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... |