QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#691273 | #5455. TreeScript | Esouling# | WA | 0ms | 3612kb | C++17 | 1.1kb | 2024-10-31 10:40:44 | 2024-10-31 10:40:46 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> deg(n + 1);
std::vector<std::vector<int>> adj(n + 1);
for (int i = 1; i <= n; i++) {
int p;
std::cin >> p;
adj[p].emplace_back(i);
deg[p]++;
}
int ans = 0, more = 0;
auto dfs = [&](auto self, int u, int f) -> void {
if (deg[u] > 0) {
if (more > 0) {
more--;
} else {
ans++;
}
}
std::sort(adj[u].begin(), adj[u].end(), [&](int i, int j) {
return deg[i] < deg[j];
});
for (const auto &v : adj[u]) {
if (v == f) {
continue;
}
--deg[u];
if (!deg[u]) {
more++;
}
self(self, v, u);
}
};
dfs(dfs, 1, 0);
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3612kb
input:
2 3 0 1 2 7 0 1 2 2 1 4 1
output:
1 1
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'