This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "race.h"
// #include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
struct node {
int sum = 0, node = 0, nodes = 0;
};
int best_path(int n, int k, int h[][2], int l[])
{
vector<vector<pair<int, int>>> graph(n);
vector<int> cnt(n);
for(int i = 0; i < n - 1; i ++){
graph[h[i][0]].push_back({h[i][1], l[i]});
graph[h[i][1]].push_back({h[i][0], l[i]});
cnt[h[i][0]] ++;
cnt[h[i][1]] ++;
}
int ans = 1e7;
for(int i = 0; i < n; i ++){
if(cnt[i] == 1){
queue<node> q;
vector<bool> used(n);
vector<vector<int>> a(n);
q.push({0, i, 0});
used[i] = true;
while (!q.empty())
{
node x = q.front();
q.pop();
while (x.sum >= k)
{
if (x.sum == k)
ans = min(ans, x.nodes);
int cur = x.node, y = x.nodes, j = 0, cur1 = x.node;
while (y)
{
if ((y & 1))
cur = a[cur][j];
y /= 2;
j++;
}
y = x.nodes - 1;
j = 0;
while (y)
{
if ((y & 1))
cur1 = a[cur1][j];
y /= 2;
j++;
}
for (auto n : graph[cur])
{
if (n.first == cur1)
{
x.sum -= n.second;
break;
}
}
x.nodes--;
}
for (auto neighbour : graph[x.node])
{
if (!used[neighbour.first])
{
q.push({x.sum + neighbour.second, neighbour.first, x.nodes + 1});
used[neighbour.first] = true;
a[neighbour.first].push_back(x.node);
for (int i = 1, y = 2; y <= x.nodes + 1; i++, y <<= 1)
{
a[neighbour.first].push_back(a[a[neighbour.first][i - 1]][i - 1]);
}
}
}
}
a.clear();
used.clear();
}
}
if(ans == 1e7)
return -1;
else
return 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... |