1004 Counting Leaves (30point(s)
2020-02-28 本文已影响0人
iphelf
树,DFS。
关键符号说明:
Symbol | Explanation |
---|---|
rt | Root node of the tree being DFSed on |
d | Depth |
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN=100;
vector<int> child[MAXN];
int ans[MAXN];
int depth;
void dfs(int rt,int d){
depth=max(depth,d+1);
if(child[rt].empty()){
ans[d]++;
return;
}
for(int i=0;i<child[rt].size();i++)
dfs(child[rt][i],d+1);
}
int main(void) {
// freopen("in.txt","r",stdin);
int n,m,k,r,c;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&r,&k);
for(int i=0;i<k;i++){
scanf("%d",&c);
child[r].push_back(c);
}
}
memset(ans,0,sizeof ans);
depth=0;
dfs(1,0);
for(int i=0;i<depth;i++) printf("%d%c",ans[i],i==depth-1?'\n':' ');
return 0;
}
/*
Sample Input:
2 1
01 1 02
Sample Output:
0 1
*/