이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
int best_path(int n, int k, int h[][2], int l[]){
vector<vector<array<int, 2>>> adj(n);
for(int i = 0; i < n; ++i){
int u = h[i][0], v = h[i][1], w = l[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
constexpr int INF = 1e9 + 7;
ll ans = INF;
map<ll, ll> mp[n];
function<void(int, int, ll, ll)> dfs = [&](int u, int p, ll dis, ll dep){
mp[u][dis] = dep;
for(auto &[v, w] : adj[u]){
if(v == p) continue;
dfs(v, u, dis + w, dep + 1);
if(mp[u].size() < mp[v].size()){
swap(mp[u], mp[v]);
}
for(auto &[ndis, ndep] : mp[v]){
ll target = k + 2 * dis - ndis;
if(mp[u].count(target))
ans = min(ans, mp[u][target] + ndep - 2 * dep);
}
for(auto &[ndis, ndep] : mp[v]){
if(!mp[u].count(ndis)){
mp[u][ndis] = ndep;
}else{
mp[u][ndis] = min(mp[u][ndis], ndep);
}
}
}
};
dfs(0, -1, 0, 0);
if(ans == INF) ans = -1;
return ans;
}
/*
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
vector<vector<array<int, 2>>> adj(n);
for(int i = 0; i < n - 1; ++i){
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
constexpr int INF = 1e9 + 7;
ll ans = INF;
map<ll, ll> mp[n];
function<void(int, int, ll, ll)> dfs = [&](int u, int p, ll dis, ll dep){
mp[u][dis] = dep;
for(auto &[v, w] : adj[u]){
if(v == p) continue;
dfs(v, u, dis + w, dep + 1);
if(mp[u].size() < mp[v].size()){
swap(mp[u], mp[v]);
}
for(auto &[ndis, ndep] : mp[v]){
ll target = k + 2 * dis - ndis;
if(mp[u].count(target))
ans = min(ans, mp[u][target] + ndep - 2 * dep);
}
for(auto &[ndis, ndep] : mp[v]){
if(!mp[u].count(ndis)){
mp[u][ndis] = ndep;
}else{
mp[u][ndis] = min(mp[u][ndis], ndep);
}
}
}
};
dfs(0, -1, 0, 0);
if(ans == INF) ans = -1;
cout << ans << '\n';
}
*/
# | 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... |