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 <bits/stdc++.h>
#define forr(_a,_b,_c) for(_a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(_a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(_a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const ll oo = 1e9;
const ll mod = 1e9 + 7;
deque <ll> s[N];
vector <pll> a[N];
ll ans, h[N], f[N];
ll k;
void dfs (int u, int p){
f[u] = 0;
//cout << u << endl;
for (pii e : a[u]){
int v = e.st;
if (v != p){
h[v] = e.nd;
dfs(v,u);
if (s[v].size() > s[u].size())
swap(s[v],s[u]), swap (f[u], f[v]);
for (int j = 0; j < s[v].size(); j++){
int d = s[v].size() - j;
if (k - d > 0 && k - d <= s[u].size()){
ans = min (ans, s[v][j] + s[u][s[u].size() - (k - d)] + f[u] + f[v]);
}
}
for (int j = 0; j < s[v].size(); j++)
s[u][s[u].size() - (s[v].size() - j)] = min (s[u][s[u].size() - (s[v].size() - j)], s[v][j] - f[u] + f[v]);
s[v].clear();
}
}
if (h[u] != 0)
s[u].pb(-f[u]);
h[u] = min (h[u], k + 1);
for (int i = 1; i < h[u]; i++){
s[u].pb(oo);
}
f[u]++;
if (k <= s[u].size()){
ans = min (ans, s[u][s[u].size() - k] + f[u]);
}
while ((s[u].size()) > k + 1)
s[u].pop_front();
}
int n,i,u,v,w;
int best_path (int nn, int kk, int edge[][2], int ww[]){
ans = oo;
n = nn;
k = kk;
if (k == 1)
return 0;
forf (i,0,n - 1){
u = edge[i][0];
v = edge[i][1];
w = ww[i];
a[u].pb({v,w});
a[v].pb({u,w});
}
dfs(0,0);
return ((ans <= (n - 1)) ? ans : -1);
}
Compilation message (stderr)
race.cpp: In function 'void dfs(int, int)':
race.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int j = 0; j < s[v].size(); j++){
| ~~^~~~~~~~~~~~~
race.cpp:40:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (k - d > 0 && k - d <= s[u].size()){
| ~~~~~~^~~~~~~~~~~~~~
race.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int j = 0; j < s[v].size(); j++)
| ~~^~~~~~~~~~~~~
race.cpp:56:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (k <= s[u].size()){
| ~~^~~~~~~~~~~~~~
race.cpp:59:23: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
59 | while ((s[u].size()) > k + 1)
| ~~~~~~~~~~~~~~^~~~~~~
# | 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... |