QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546412 | #5455. TreeScript | Sunlight9 | WA | 17ms | 3856kb | C++20 | 1.5kb | 2024-09-04 00:52:46 | 2024-09-04 00:52:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector<vector<int> > edge(n + 1);
vector<int> fa(n + 1);
int st;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
if (x) {
fa[i] = x;
edge[x].push_back(i);
} else {
st = i;
}
}
vector<int> num(n + 1), dep(n + 1), all(n + 1);
auto dfs = [&](auto &&self, int u) -> void {
dep[u] = dep[fa[u]] + 1;
all[dep[u]]++;
if (!edge[u].empty()) {
num[dep[u]]++;
}
for (auto v : edge[u]) {
self(self, v);
}
};
int ans = 1;
vector<int> pd(n + 1);
auto dfs1 = [&](auto &&self, int u) -> void {
if (edge[u].empty()) return;
if (!pd[dep[u]] && (all[dep[u]] == 1 || num[dep[u]] > 1) && (int)edge[u].size() > 1) {
ans++;
pd[dep[u]] = 1;
}
for (auto v : edge[u]) {
self(self, v);
}
};
dfs(dfs, st);
dfs1(dfs1, st);
// for (int i = 1; i <= 5; ++i) {
// cerr << num[i] << " num\n";
// }
cout << ans << '\n';
}
int main() {
cin.tie(nullptr) -> sync_with_stdio(false);
int _;
cin >> _;
while (_--) solve();
return 0;
}
/*
2
3
0 1 2
7
0 1 2 2 1 4 1
*/
/*
1
4
0 1 2 2
*/
/*
1
7
0 1 1 3 1 5 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3856kb
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: 17ms
memory: 3788kb
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:
9 11 4 9 7 7 10 10 8 11 8 10 14 9 7 6 11 14 6 11 13 5 9 9 9 8 11 7 8 10 2 13 9 6 11 8 6 7 6 8 9 7 11 5 7 10 11 11 8 10 7 3 10 9 11 10 7 9 7 10 12 11 4 10 10 8 7 10 13 8 11 10 11 7 11 12 3 6 9 1 1 10 13 8 7 6 11 13 11 11 11 11 8 10 5 6 11 6 9 9 13 10 10 10 10 11 5 6 11 6 11 7 6 7 10 8 5 3 7 12 5 9 5 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '9'