이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
const int N = 2e5 + 5;
vector <int> adj[N];
int n, k;
int& rev(vector <int> &a, int i) {
return a[a.size() - i - 1];
}
vector <int> dfs(int u) {
vector <int> res(1, 1);
for (int v : adj[u]) {
auto add = dfs(v);
add.push_back(add.back());
if (add.size() > res.size())
swap(add, res);
for (int i = 0; i < add.size(); i++) {
int a = rev(add, i) + (k - i < res.size() ?
rev(res, max(i, k - i)) : 0);
int b = rev(res, i) + (k - i < add.size() ?
rev(add, max(i, k - i)) : 0);
cmax(rev(res, i), max(a, b));
}
for (int i = add.size() - 1; ~i; i--)
if (i + 1 < res.size())
cmax(rev(res, i), rev(res, i + 1));
}
return res;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int j; cin >> j;
adj[j].push_back(i);
}
cout << dfs(0).back() << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp: In function 'std::vector<int> dfs(int)':
catinatree.cpp:25:27: 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 < add.size(); i++) {
| ~~^~~~~~~~~~~~
catinatree.cpp:26:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | int a = rev(add, i) + (k - i < res.size() ?
| ~~~~~~^~~~~~~~~~~~
catinatree.cpp:28:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | int b = rev(res, i) + (k - i < add.size() ?
| ~~~~~~^~~~~~~~~~~~
catinatree.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if (i + 1 < res.size())
| ~~~~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |