이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("03")
#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;
int updates_count = 0;
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[]){
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){
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[updates_count] = {l, h}; updates_count++;
}
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);
}
b[c] = 0;
}
void centroid_decomposition(int c, vector <int> g[], vector <int> w[], bool b[], int s[], pair<int, int> d[]){
siz = 0; 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);
for (int j = 0; j < updates_count; j++)d[updates[j].first].first = min(d[updates[j].first].first, updates[j].second);
updates_count = 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]);
}
updates.resize(N);
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;
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void get_size(int, std::vector<int>*, bool*, int*)':
race.cpp:17:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < g[c].size(); i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, std::vector<int>*, bool*, int*)':
race.cpp:28:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | 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)':
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:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | 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... |