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, node, nodes;
};
int best_path(int n, int k, int h[][2], int l[])
{
vector<vector<pair<int, int>>> graph(n);
int root;
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]] ++;
}
for(int i = 0; i < n; i ++){
if(cnt[i] == 1){
root = i;
break;
}
}
queue<node> q;
vector<bool> used(n);
vector<vector<int>> a(n);
q.push({0, root, 0});
used[root] = true;
int ans = 1e7;
while(!q.empty()){
node x = q.front();
q.pop();
while(x.sum > k){
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 --;
}
if (x.sum == k)
ans = min(ans, 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]);
}
}
}
}
if(ans == 1e7)
return -1;
else
return ans;
}
Compilation message (stderr)
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:30:11: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | q.push({0, root, 0});
| ~~~~~~^~~~~~~~~~~~~~
# | 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... |