# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1199350 | Valters07 | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "race.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define ld long double
#define en exit(0);
#define pb push_back
#define fi first
#define se second
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1005;
vector<pair<int,int> > g[N];
int dist[N], len[N];
void dfs(int u, int p)
{
for(auto [v, w] : g[u])
{
if(v != p)
{
dist[v] = dist[u] + w;
len[v] = len[u] + 1;
dfs(v, u);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i = 0;i < N - 1;i++)
{
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});s
}
int res = -1;
for(int i = 0;i < N;i++)
{
dist[i] = len[i] = 0;
dfs(i, i);
for(int j = 0;j < N;j++)
if(dist[j] == K && (res == -1 || len[j] < res))
res = len[j];
}
return res;
}