이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pp;
const int MAXN = 2 * 1e5 + 100;
const int INF = 2 * 1e9;
struct mapa{
int val = INF;
};
int n; int k;
vector<pp> v[MAXN];
int p[MAXN] = { 0 };
int vel[MAXN] = { 0 };
map<int, mapa> m;
vector<int> s;
int sol = INF;
int cnt = 0;
int dfs(int x, int depth, int sum, int val, int k){
p[x] = 1; vel[x] = 0;
if (sum > k)
return 0;
if (val == 1 && sum <= k){
int zb = depth + m[k - sum].val - 1;
sol = min(sol, zb);
}
else if (val == 2 && sum <= k){
s.push_back(sum);
m[sum].val = min(m[sum].val, depth);
}
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y]){
int rt = dfs(y, depth + 1, sum + v[x][i].second, val, k);
if (val == 0)
vel[x] += rt;
}
}
if (val == 0)
++vel[x];
p[x] = 0;
if (val == 0)
return vel[x];
return 0;
}
int centr(int x){
p[x] = 1; pp maks = {-1, -1};
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y] && vel[y] > maks.second)
maks = {y, vel[y]};
}
if (maks.second <= cnt / 2){
p[x] = 0; return x;
}
else{
int rjes = centr(maks.first);
p[x] = 0; return rjes;
}
}
void solve(int x, int K){
s.clear(); m[0].val = 1;
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y]){
dfs(y, 2, v[x][i].second, 1, K); dfs(y, 2, v[x][i].second, 2, K);
}
}
while (!s.empty()){
m[s.back()].val = INF; s.pop_back();
}
return;
}
void decompose(int x, int K){
cnt = 0; dfs(x, 1, 0, 0, K); cnt = vel[x];
int root = centr(x); p[root] = 1;
solve(root, K);
for (int i = 0; i < v[root].size(); ++i){
int y = v[root][i].first;
if (!p[y]) decompose(y, K);
}
}
int best_path(int N, int K, int H[MAXN][2], int L[MAXN]){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 0; i < N - 1; ++i){
v[H[i][0]].push_back({H[i][1], L[i]});
v[H[i][1]].push_back({H[i][0], L[i]});
}
decompose(0, K);
if (sol == INF)
return -1;
else return (sol - 1);
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'int dfs(int, int, int, int, int)':
race.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'int centr(int)':
race.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int, int)':
race.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < v[root].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... |