이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstdio>
#include <map>
#include <functional>
#include <vector>
//#include "race.h"
using namespace std;
using ll = long long;
using ld = long double;
constexpr int N = 2e5 + 5;
constexpr int Inf = 1e9 + 7;
ll d[N];
vector<pair<int, int>> adj[N];
map<ll, int> cnt[N];
int ranks[N];
int Get(map<ll, int> &cnt, ll x)
{
auto j = cnt.find(x);
return j == cnt.end() ? Inf + 5 : j->second;
}
void Update(map<ll, int> &cnt, ll x, int y)
{
auto j = cnt.find(x);
if (j == cnt.end())
cnt[x] = y;
else
j->second = min(j->second, y);
}
int best_path(int n, int k, int h[][2], int l[])
{
int ans(Inf);
function<void(int, int)> dfs = [&](int v, int p)
{
cnt[v][d[v]] = ranks[v];
for (auto i : adj[v])
if (i.first != p)
{
d[i.first] = d[v] + i.second;
ranks[i.first] = ranks[v] + 1;
dfs(i.first, v);
if (cnt[v].size() < cnt[i.first].size())
swap(cnt[i.first], cnt[v]);
for (auto j : cnt[i.first])
if (j.first - d[v] <= k)
ans = min(ans, Get(cnt[v], k - (j.first - d[v]) + d[v]) + j.second - 2 * ranks[v]);
for (auto j : cnt[i.first])
if (j.first - d[v] <= k)
Update(cnt[v], j.first, j.second);
cnt[i.first].clear();
}
// cerr << v << ": " << ans << "\n";
};
for (int i = 0; i < n - 1; ++i)
{
adj[h[i][0] + 1].emplace_back(h[i][1] + 1, l[i]);
adj[h[i][1] + 1].emplace_back(h[i][0] + 1, l[i]);
}
ranks[1] = 1;
dfs(1, -1);
return ans >= Inf ? -1 : ans;
}
# | 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... |