1004 Counting Leaves (30 分)
思路
数每一层的叶子结点,秒杀
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int N,K;
vector<int> vec[maxn];
int num[maxn]={0};
int max_depth = 0;
void DFS(int index ,int depth)
{
max_depth = max(max_depth,depth);
if (vec[index].size()==0)
num[depth]+=1;
for(int i =0;i<vec[index].size();i++)
{
DFS(vec[index][i],depth+1);
}
}
int main()
{
int id,number,child;
cin>>N>>K;
for(int i = 0;i<K;i++)
{
cin>>id>>number;
for(int j=0;j<number;j++)
{
cin>>child;
vec[id].push_back(child);
}
}
DFS(1,0);
for(int i =0;i<max_depth;i++)
cout<<num[i]<<" ";
cout<<num[max_depth]<<endl;
}