#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];
}
}
solution 2(bfs)
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 110;
vector<int> vec[N];
queue<int> q, que[N];
int depth, maxdepth;
int dep[N], root[N];
void bfs()
{
q.push(1);
dep[1] = 0;
while (q.size())
{
int idx = q.front();
q.pop();
maxdepth = max(maxdepth, dep[idx]);
if (vec[idx].size() == 0)
{
root[dep[idx]]++;
}
for (int i = 0; i < vec[idx].size(); i++)
{
q.push(vec[idx][i]);
dep[vec[idx][i]] = dep[idx] + 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);
}
}
bfs();
cout << root[0];
for (int i = 1; i <= maxdepth; i++)
{
cout << " " << root[i];
}
}