solution.cpp 808 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int e[300], h[300], nex[300];
int idx;
int cnt[300], mx_dep;
void add(int u, int v)
{
    e[idx] = v, nex[idx] = h[u], h[u] = idx++;
}
void dfs(int u, int dep)
{
    if (h[u] == -1) //叶子结点
    {
        cnt[dep]++;
        mx_dep = max(mx_dep, dep);
        return;
    }
    for (int i = h[u]; ~i; i = nex[i])
        dfs(e[i], dep + 1);
}

int main()
{
    int n, m;
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++)
    {
        int id, k;
        cin >> id >> k;
        while (k--)
        {
            int b;
            cin >> b;
            add(id, b);
        }
    }
    dfs(1, 0);
    cout << cnt[0];
    for (int i = 1; i <= mx_dep; i++)
        cout << ' ' << cnt[i];

    return 0;
}