PAT甲级1004
作者:互联网
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 110;
int depth, maxdepth;
int root[N];
vector<int> vec[N];
void dfs(int idx, int depth)
{
if (vec[idx].size() == 0)
{
maxdepth = max(maxdepth, depth);
root[depth]++;
return;
}
for (int i = 0; i < vec[idx].size(); i++)
{
dfs(vec[idx][i], depth + 1);
}
}
int main()
{
int n, m;
cin >> n >> m;
while (m--)
{
int node, k;
cin >> node >> k;
while (k--)
{
int x;
cin >> x;
vec[node].push_back(x);
}
}
dfs(1, 0);
cout << root[0];
for (int i = 1; i <= maxdepth; i++)
{
cout << " " << root[i];
}
}
标签:include,PAT,idx,int,dfs,depth,甲级,vec,1004 来源: https://blog.csdn.net/qq_34832548/article/details/120089568