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;
void get_size(int c, vector <int> g[], bool b[], int s[]){
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, g, b, s);
s[c] += s[h];
}
}
b[c] = 0;
}
void dfs(int c, vector <int> g[], bool b[], int s[]){
if (current == -1)return;
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, g, b, s);
m = max(m, s[h]);
}
}
b[c] = 0;
if (m <= siz)current = c;
}
void solve(int c, vector <int> g[], vector <int> w[], bool b[], pair<int, int> d[], ll l, int h, bool filling){
if (l > k)return;
b[c] = 1;
if (filling){
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;}
}
else {
d[l].first = min(d[l].first, h);
}
for (int i = 0; i < g[c].size(); i++){
if (!b[g[c][i]])solve(g[c][i], g, w, b, d, l+w[c][i], h+1, filling);
}
b[c] = 0;
}
void centroid_decomposition(int c, vector <int> g[], vector <int> w[], bool b[], int s[], pair<int, int> d[]){
siz = 0; current = -1; get_size(c, g, b, s); siz /= 2; dfs(c, g, b, s);
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, g, w, b, d, w[r][i], 1, 1);
solve(v, g, w, b, d, w[r][i], 1, 0);
}
}
timer++;
for (int i = 0; i < g[r].size(); i++){
if (!b[g[r][i]])centroid_decomposition(g[r][i], g, w, b, s, d);
}
}
int best_path(int N, int K, int H[][2], int L[]){
vector <int> g[N], w[N]; k = K;
pair<int, int> d[k+1]; 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]);
}
bool b[N]; for (int i = 0; i < N; i++)b[i] = 0;
int s[N];
centroid_decomposition(0, g, w, b, s, d);
if (sol == INF)sol = -1;
return sol;
}
Compilation message (stderr)
race.cpp: In function 'void get_size(int, std::vector<int>*, bool*, int*)':
race.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, std::vector<int>*, bool*, int*)':
race.cpp:25:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int, std::vector<int>*, std::vector<int>*, bool*, std::pair<int, int>*, ll, int, bool)':
race.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void centroid_decomposition(int, std::vector<int>*, std::vector<int>*, bool*, int*, std::pair<int, int>*)':
race.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < g[r].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | 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... |