QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#691337 | #5455. TreeScript | Esouling# | WA | 21ms | 3908kb | C++17 | 2.2kb | 2024-10-31 11:00:12 | 2024-10-31 11:00:20 |
Judging History
answer
// #include <bits/stdc++.h>
// using i64 = long long;
// void solve() {
// int n;
// std::cin >> n;
// std::vector<int> A(n + 1);
// for (int i = 1; i <= n; i++) {
// std::cin >> A[i];
// }
// std::vector<int> tab(n + 1);
// std::vector<int> nxt(n + 1);
// for (int i = n; i >= 1; i--) {
// if (tab[A[i]] == 0) {
// nxt[i] = 0;
// tab[A[i]] = 1;
// } else {
// nxt[i] = 1;
// }
// }
// int cnt = 0;
// int ans = 0;
// std::vector<int> vis(n + 1);
// for (int i = 1; i <= n; i++) {
// if ()
// if (nxt[i]) {
// vis[i] = 1;
// cnt++;
// }
// ans = std::max(ans, cnt);
// }
// 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;
// }
#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 (more > 0) {
more--;
} else {
ans++;
}
if (deg[u] == 0) {
assert(adj[u].empty());
more++;
}
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: 100
Accepted
time: 0ms
memory: 3500kb
input:
2 3 0 1 2 7 0 1 2 2 1 4 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: -100
Wrong Answer
time: 21ms
memory: 3908kb
input:
1000 197 0 1 1 2 1 4 1 5 8 3 5 1 4 7 12 14 4 7 10 9 12 11 16 10 21 19 22 17 25 13 28 9 5 15 26 26 33 25 15 1 35 6 32 17 37 8 19 43 19 27 29 9 30 6 31 27 35 35 37 13 28 38 57 31 38 8 22 14 33 9 18 62 52 37 10 19 22 60 54 12 38 59 64 65 80 82 28 60 85 78 27 25 71 14 52 6 59 14 87 32 33 41 59 41 88 38 ...
output:
5 6 3 4 3 4 5 6 4 4 5 4 8 6 3 4 5 7 4 7 7 3 5 5 6 5 7 3 4 6 2 7 5 5 6 5 4 4 3 5 6 4 5 3 4 5 6 5 4 6 4 3 5 5 7 5 5 5 5 5 7 6 3 5 5 4 4 6 7 4 4 6 7 4 6 5 3 3 5 1 1 7 6 4 4 4 7 7 7 6 6 6 5 5 4 4 6 4 5 5 8 6 5 7 6 6 3 3 6 5 6 5 3 4 6 5 3 2 4 6 3 7 3 7 5 5 5 4 7 6 7 4 7 4 6 3 7 5 4 4 3 4 3 2 6 6 4 2 5 9 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '5'