# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1150981 | Shadow1 | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << '\n';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
const int maxn = 2e5 + 5;
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(u == 1) show(x.fir);
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], mp[u][x.fir] + 1);
else
mp[u][w+x.fir] = mp[u][x.fir] + 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);
}