QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#691273#5455. TreeScriptEsouling#WA 0ms3612kbC++171.1kb2024-10-31 10:40:442024-10-31 10:40:46

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 10:40:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3612kb
  • [2024-10-31 10:40:44]
  • 提交

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;
}

詳細信息

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'