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>
using namespace std;
typedef long long ll;
const int INF = 1e6;
int current, siz, k, timer = 0, sol = INF;
vector <pair<int, int>> updates;
vector <pair<int, int>> d;
vector <vector <int>> g, w;
vector <int> s;
vector <bool> b;
void get_size(int c){
s[c] = 1; b[c] = 1; siz++;
for (int i = 0; i < g[c].size(); i++){
if (!b[g[c][i]]){
int h = g[c][i]; get_size(h);
s[c] += s[h];
}
}
b[c] = 0;
}
void dfs(int c){
b[c] = 1; int m = 0;
for (int i = 0; i < g[c].size(); i++){
if (!b[g[c][i]]){
int h = g[c][i]; dfs(h);
m = max(m, s[h]);
}
}
b[c] = 0;
if (m <= siz)current = c;
}
void solve(int c, int l, int h){
b[c] = 1;
if (l <= k){
if (d[k-l].second < timer){d[k-l].second = timer; d[k-l].first = INF;}
sol = min(sol, d[k-l].first+h);
if (l == k)sol = min(sol, h);
if (d[l].second < timer){d[l].second = timer; d[l].first = INF;}
updates.push_back({l, h});
}
for (int i = 0; i < g[c].size(); i++){
if (!b[g[c][i]])solve(g[c][i], min(l+w[c][i], INF), h+1);
}
b[c] = 0;
}
void centroid_decomposition(int c){
siz = 0; get_size(c); siz /= 2; dfs(c);
int r = current; b[r] = 1;
for (int i = 0; i < g[r].size(); i++){
int v = g[r][i];
if (!b[v]){
solve(v, w[r][i], 1);
for (int j = 0; j < updates.size(); j++)d[updates[j].first].first = min(d[updates[j].first].first, updates[j].second);
updates.clear();
}
}
timer++;
for (int i = 0; i < g[r].size(); i++){
if (!b[g[r][i]])centroid_decomposition(g[r][i]);
}
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
d.resize(k+1); g.resize(N); w.resize(N); b.resize(N); s.resize(N);
for (int i = 0; i <= k; i++)d[i] = {INF, 0};
for (int i = 0; i < N-1; i++){
g[H[i][0]].push_back(H[i][1]); g[H[i][1]].push_back(H[i][0]);
w[H[i][0]].push_back(L[i]); w[H[i][1]].push_back(L[i]);
}
for (int i = 0; i < N; i++)b[i] = 0;
centroid_decomposition(0);
if (sol == INF)sol = -1;
return sol;
}
Compilation message (stderr)
race.cpp: In function 'void get_size(int)':
race.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int)':
race.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int, int, int)':
race.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void centroid_decomposition(int)':
race.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 0; i < g[r].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < updates.size(); j++)d[updates[j].first].first = min(d[updates[j].first].first, updates[j].second);
| ~~^~~~~~~~~~~~~~~~
race.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = 0; i < g[r].size(); i++){
| ~~^~~~~~~~~~~~~
# | 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... |