2022.1.28 练习 PAT甲 1004 Counting Leaves (原题链接)
题解如下:
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE=110;
int num[MAX_SIZE]={0};
vector<int> Node[MAX_SIZE];
int max_high=0;
void DFS(int index,int high)
{
if(Node[index].size()==0)
{
if(high>max_high)
max_high=high;
num[high]++;
}
for(unsigned int i=0;i<Node[index].size();i++)
{
DFS(Node[index][i],high+1);
}
}
int main()
{
std::ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int id,k;
cin>>id>>k;
for(int j=0;j<k;j++)
{
int tmp;
cin>>tmp;
Node[id].push_back(tmp);
}
}
DFS(1,1);
for(int i=1;i<=max_high;i++)
{
cout<<num[i];
if(i<max_high)
cout<<" ";
}
return 0;
}
|