From 2bbd7d55231feecb8430ee23d0622e91169c5911 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 31 Aug 2022 14:20:42 +0800 Subject: [PATCH] =?UTF-8?q?T263133=20=E6=8B=86=E7=BD=91=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/85559526 --- Luogu/T263133/T263133.cpp | 69 ++++++++++++++++++++++++++++++++++ Luogu/T263133/samples/tree.in | 3 ++ Luogu/T263133/samples/tree.out | 3 ++ 3 files changed, 75 insertions(+) create mode 100644 Luogu/T263133/T263133.cpp create mode 100644 Luogu/T263133/samples/tree.in create mode 100644 Luogu/T263133/samples/tree.out diff --git a/Luogu/T263133/T263133.cpp b/Luogu/T263133/T263133.cpp new file mode 100644 index 00000000..40b7c2d4 --- /dev/null +++ b/Luogu/T263133/T263133.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int t, n, k; +std::vector top, g[N]; +bool vis[N]; + +void dfs(int u) { + vis[u] = true; + + for (int v : g[u]) { + if (!vis[v]) dfs(v); + } + + top.push_back(u); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> t; + + while (t--) { + top.clear(); + std::fill_n(g, N, std::vector()); + std::fill_n(vis, N, false); + + int ans = 0; + + cin >> n >> k; + + for (int i = 2, x; i <= n; i++) { + cin >> x; + + g[i].push_back(x); + g[x].push_back(i); + } + + dfs(1); + + std::fill_n(vis, N, false); + + for (int u : top) { + if (k <= 1) break; + + if (vis[u]) continue; + + for (int v : g[u]) { + if (vis[v]) continue; + + vis[u] = vis[v] = true; + ans++; + k -= 2; + } + } + + cout << ans + k << endl; + } + + return 0; +} diff --git a/Luogu/T263133/samples/tree.in b/Luogu/T263133/samples/tree.in new file mode 100644 index 00000000..ed28e233 --- /dev/null +++ b/Luogu/T263133/samples/tree.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7464603693c092c589573d1deb3ba072c9d2e564230d10d4d10b0502cdff249d +size 75948 diff --git a/Luogu/T263133/samples/tree.out b/Luogu/T263133/samples/tree.out new file mode 100644 index 00000000..2fd0e145 --- /dev/null +++ b/Luogu/T263133/samples/tree.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9603c54873cb21af523745c033b2d01ab7feebd5eec105232a09e42ea5425a2b +size 50