题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184
没啥好说的。。就是一开始根结点序号弄错了,还有输出循环时下标漏了一个,改完就AC了
代码
#include<algorithm>#include<vector>#include<iostream>using namespace std;const int maxn = 110;struct Node{int depth;vector<int> child;}node[maxn];int hash_table[maxn] = {0};int n, m, tree_depth = 0;void DFS(int root, int depth){if(node[root].child.size() == 0){tree_depth = max(depth, tree_depth);hash_table[depth]++;return;}for(int i = 0; i < node[root].child.size(); i++){int child = node[root].child[i];DFS(child, depth + 1);}}int main(){int node_id, child_num, child_temp;scanf("%d%d", &n, &m);for(int i = 0; i < m; i++){scanf("%d%d", &node_id, &child_num);for(int i = 0; i < child_num; i++){scanf("%d",&child_temp);node[node_id].child.push_back(child_temp);}}DFS(1, 0);for(int i = 0; i <= tree_depth; i++){printf("%d", hash_table[i]);if(i != tree_depth) printf(" ");}return 0;}
