1004. Counting Leaves (30)

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn = 105;
 5 struct Node
 6 {
 7     int father;
 8     int level;
 9     bool NoChild;
10 };
11 
12 Node v[maxn];
13 int level[maxn];
14 
15 int main()
16 {
17     int N,M,i,j,c,ID,child;
18     int MAXLevel = 1;
19     scanf("%d%d",&N,&M);
20     for(i=0;i<100;i++)
21     {
22         v[i].father=0;
23         v[i].level=0;
24         v[i].NoChild=1;
25     }
26     for(i=0;i<M;i++)
27     {
28         scanf("%d%d",&ID,&c);
29         v[ID].NoChild=0;
30         for(j=0;j<c;j++)
31         {
32             scanf("%d",&child);
33             v[child].father = ID;
34         }
35     }
36 
37     v[1].level=1;
38     for(i=1;i<=N;i++)
39         for(j=1;j<=N;j++)
40         {
41             //如果其根节点是i
42             if(v[j].father==i)
43             {
44                 v[j].level = v[v[j].father].level+1;
45                 if(v[j].level > MAXLevel)
46                     MAXLevel = v[j].level;
47             }
48         }
49 
50     for(i=1;i<=N;++i)
51         if(v[i].NoChild==1)
52             level[v[i].level]++;
53     for(i=1;i<MAXLevel;++i)
54         printf("%d ",level[i]);
55     printf("%d\n",level[i]);
56     return 0;
57 }

 

posted @ 2018-05-03 22:03  ouyang_wsgwz  阅读(135)  评论(0编辑  收藏  举报