이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 1500 + 5, INF = 1e9;
int n, k;
array<vector<int>, MAX_N> adj;
/************************************/
array<array<int, MAX_N>, MAX_N> floyd;
void precomp() {
for (int u = 1; u <= n; u++) {
fill(floyd[u].begin(), floyd[u].end(), INF);
for (int v : adj[u]) floyd[u][v] = 1;
floyd[u][u] = 0;
}
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
for (int w = 1; w <= n; w++)
floyd[u][v] = min(floyd[u][v], floyd[u][w] + floyd[w][v]);
}
/************************************/
array<int, MAX_N> siz;
array<bool, MAX_N> del;
void update_siz(int u, int par = -1) {
siz[u] = 1;
for (int v : adj[u]) {
if (v == par) continue;
if (del[v]) continue;
update_siz(v, u), siz[u] += siz[v];
}
}
int find_cent(int u, int tot_siz, int par = -1) {
for (int v : adj[u]) {
if (v == par) continue;
if (del[v]) continue;
if (siz[v] * 2 > tot_siz) return find_cent(v, tot_siz, u);
}
return u;
}
array<vector<int>, MAX_N> ancs; // Reversed
array<unordered_map<int, int>, MAX_N> dist, subcent;
array<unordered_map<int, vector<pii>>, MAX_N> dists; // [itself] = whole thing
void find_dists(int u, int d, int par, int cent, int sub) {
dist[cent][u] = d, dists[cent][sub].push_back({d, u});
subcent[cent][u] = sub;
ancs[u].push_back(cent);
for (int v : adj[u]) {
if (v == par) continue;
if (del[v]) continue;
find_dists(v, d + 1, u, cent, sub);
}
}
void build(int u = 1) {
update_siz(u);
int cent = find_cent(u, siz[u]);
dist[cent][cent] = 0, dists[cent][cent].push_back({0, cent});
ancs[cent].push_back(cent);
for (int v : adj[cent]) {
if (del[v]) continue;
find_dists(v, 1, cent, cent, v);
sort(dists[cent][v].begin(), dists[cent][v].end());
dists[cent][cent].insert(dists[cent][cent].begin(), dists[cent][v].begin(), dists[cent][v].end());
}
sort(dists[cent][cent].begin(), dists[cent][cent].end());
del[cent] = true;
for (int v : adj[cent])
if (!del[v]) build(v);
}
int mini_query(int u, int d, int exc) {
int i = lower_bound(dists[u][u].begin(), dists[u][u].end(), make_pair(d, -1)) - 1 - dists[u][u].begin(); // First index with dist < d
int j = (exc == -1) ? -1 : lower_bound(dists[u][exc].begin(), dists[u][exc].end(), make_pair(d, -1)) - 1 - dists[u][exc].begin();
int ans = i + 1 - (j + 1);
return ans;
}
int query(int u) {
reverse(ancs[u].begin(), ancs[u].end());
int ans = 0;
for (int i = 0; i < ancs[u].size(); i++) {
int v = ancs[u][i];
int new_ans = (i == 0) ? mini_query(v, k, -1) : mini_query(v, k - dist[v][u], subcent[v][u]);
ans += new_ans;
}
// cout << u << ": ";
// for (int v : ancs[u]) cout << v << " ";
// cout << endl;
// cout << u << ": " << ans << endl;
return ans;
}
vector<pii> order;
array<bool, MAX_N> used;
int comp() {
for (int u = 1; u <= n; u++) order.push_back({query(u), u});
sort(order.begin(), order.end());
int ans = 0;
for (pii x : order) {
int u = x.second;
if (used[u]) continue;
ans++;
for (int v = 1; v <= n; v++)
if (floyd[u][v] < k) used[v] = true;
}
return ans;
}
int main() {
// freopen("cat.in", "r", stdin);
cin >> n >> k; assert(n <= 1500);
for (int u = 2; u <= n; u++) {
int v; cin >> v; v++;
adj[u].push_back(v), adj[v].push_back(u);
}
precomp();
build();
// for (int u = 1; u <= n; u++) {
// cout << u << ": ";
// for (pii x : dists[u][u]) cout << x.second << "," << x.first << " ";
// cout << endl;
// }
int ans = comp();
cout << ans << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp: In function 'int query(int)':
catinatree.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < ancs[u].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... |