题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184
没啥好说的。。就是一开始根结点序号弄错了,还有输出循环时下标漏了一个,改完就AC了

代码

  1. #include<algorithm>
  2. #include<vector>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn = 110;
  6. struct Node{
  7. int depth;
  8. vector<int> child;
  9. }node[maxn];
  10. int hash_table[maxn] = {0};
  11. int n, m, tree_depth = 0;
  12. void DFS(int root, int depth){
  13. if(node[root].child.size() == 0){
  14. tree_depth = max(depth, tree_depth);
  15. hash_table[depth]++;
  16. return;
  17. }
  18. for(int i = 0; i < node[root].child.size(); i++){
  19. int child = node[root].child[i];
  20. DFS(child, depth + 1);
  21. }
  22. }
  23. int main(){
  24. int node_id, child_num, child_temp;
  25. scanf("%d%d", &n, &m);
  26. for(int i = 0; i < m; i++){
  27. scanf("%d%d", &node_id, &child_num);
  28. for(int i = 0; i < child_num; i++){
  29. scanf("%d",&child_temp);
  30. node[node_id].child.push_back(child_temp);
  31. }
  32. }
  33. DFS(1, 0);
  34. for(int i = 0; i <= tree_depth; i++){
  35. printf("%d", hash_table[i]);
  36. if(i != tree_depth) printf(" ");
  37. }
  38. return 0;
  39. }