0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:28:47 +00:00

T263133 拆网线

https://www.luogu.com.cn/record/85559526
This commit is contained in:
Baoshuo Ren 2022-08-31 14:20:42 +08:00
parent 40c8a82aeb
commit 2bbd7d5523
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 75 additions and 0 deletions

69
Luogu/T263133/T263133.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <cmath>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int t, n, k;
std::vector<int> 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<int>());
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;
}

BIN
Luogu/T263133/samples/tree.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/T263133/samples/tree.out (Stored with Git LFS) Normal file

Binary file not shown.