ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

AT AGC004 题解【鸽】

AT AGC004 题解【鸽】

include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 1e5 + 7;

int n, m;
int cnt, ans;
int a[N];

std::vector adj[N];

void addedge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); }

int dfs(int u, int from) {
int ret = 0;
for (auto v : adj[u]) {
if (v == from)
continue;
ret = std::max(ret, dfs(v, u));
}
ret++;
if (ret == m && u != 1) {
ret = 0;
ans += (a[u] != 1);
}
return ret;
}

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

std::cin >> n >> m;
for (int i = 1; i <= n; i++) {std::cin >> a[i];
}
if (a[1] != 1) { ans++; a[1] = 1; }
for (int i = 2; i <= n; i++) {addedge(i, a[i]);
}
dfs(1, 0);
std::cout << ans << "\n";
return 0;

}

返回列表