#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pii pair<int,int>
#define fir first
#define sec second
#define sz(x) ll(x.size())
// #define int long long
const int maxn = 2e5 + 5;
unordered_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];
void dfs(int u, int p) {
for(auto &k : adj[u]) {
int w = k.sec, v = k.fir;
if(v == p) continue;
dfs(v, u);
mp[u][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][w+x.fir] > 0)
mp[u][w+x.fir] = min(mp[u][w+x.fir], x.sec + 1);
else
mp[u][w+x.fir] = x.sec + 1;
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int n = N, k = K;
for(int i=0; i<n; ++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;
for(int i=0; i<n; ++i) {
if(mp[i][k] > 0)
ans = min(ans, mp[i][k]);
}
return (ans == maxn ? -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... |