PAT1004

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child.

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes.  Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children.  For the sake of simplicity, let us fix the root ID to be 01.

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root.  The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child.  Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node.  Then we should output "0 1" in a line.

Sample Input

2 1
01 1 02

Sample Output

0 1

我的思想是:根据输入 建树,然后层序遍历找出叶子节点即可,关键在于建树,利用一个指针数组保存指向每个结点的指针,便于查找结点,例如a[i]就是只想i结点的指针!

 1 /*注意题目输入,要加以判断,例如按1 5 2到6,8 4 9-12, 2 1 7,3 1 8   程序的34到42行加以判断是否为乱序输入*/
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<iterator>
 6 using namespace std;
 7 
 8 struct Node
 9 {
10     int val;
11     vector<Node*> kids;
12 };
13 
14 int main()
15 {
16     int n, m;
17     while(cin>>n>>m)
18     {
19         Node* p[100]={NULL};
20         //记录各个节点的指针,便于访问
21         for(int i=0; i < m; ++i)
22         {
23             int ID; cin>>ID;
24             if(p[ID] == NULL)
25             {
26                 Node *n = new Node;
27                 n->val = ID;
28                 p[ID] = n;
29             }
30             int k; cin>>k;
31             for(int i=0; i < k; ++i)
32             {
33                 int id; cin>>id;
34                 if(p[id] == NULL)
35                 {
36                     Node *n = new Node;
37                     n->val = id;
38                     p[id] = n;
39                     p[ID]->kids.push_back(n);
40                 }
41                 else
42                     p[ID]->kids.push_back(p[id]);
43             }
44         }
45         //创建树
46         queue<Node> que;
47         if(p[1] == NULL)
48             cout<<1<<endl;
49         /*只有一个根节点*/
50         else
51         {
52             que.push( (*p[1]) );
53             Node null={0};
54             //此结点用来在队列中隔开每层的结点
55             que.push(null);
56             int count=0;
57             while(que.size() != 1)
58             {
59                 if(que.front().val == 0)
60                 {
61                     que.push(null);
62                     que.pop();
63                     cout<<count<<" ";
64                     count = 0;
65                 }
66                 if(que.front().kids.size() ==0)
67                     ++count;
68                 else
69                 {
70                     vector<Node*>::iterator iter = que.front().kids.begin();
71                     while(iter != que.front().kids.end())
72                     {
73                         que.push(**iter); ++iter;
74                     }
75                 }
76                 que.pop();
77             }
78             //层序遍历输出
79             cout<<count<<endl;
80         }
81     }
82     return 0;
83 }

再补充一种简单的方法,将树存成有向图,然后BFS输出,这个比较简单!

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 
 6 #define INF 9999
 7 
 8 void BFS_version(vector<vector<int>> &tree, vector<bool> &isLeaves)
 9 {
10     queue<int> q;
11     q.push(0);
12     /*-1用于在队列中隔开每层*/
13     q.push(-1);
14     int count(0);
15     while(!q.empty())
16     {
17         if(q.front() == -1 && q.size() == 1)
18         {
19             cout<<count<<endl;
20             q.pop(); break;
21         }
22         else if(q.front() == -1)
23         {
24             cout<<count<<" ";
25             count = 0;
26             q.pop();
27             q.push(-1);
28         }
29         if(isLeaves[q.front()])
30             {++count; q.pop();}
31         else
32         {
33             for(int i=0; i<tree.size(); ++i)
34                 if(tree[q.front()][i] != INF)
35                     q.push(i);
36             q.pop();
37         }
38     }
39 }
40 
41 int main()
42 {
43     int N, M;
44     while(cin>>N>>M)
45     {
46         vector<int> colum(N, INF);
47         /*标记每个结点是否是叶子结点*/
48         vector<bool> isLeaves(N, true);
49         /*将树存成有向图*/
50         vector<vector<int>> tree(N, colum);
51         for(int i=0; i<M; ++i)
52         {
53             int a, b; cin>>a>>b;
54             for(int j=0; j<b; ++j)
55             {
56                 int c; cin>>c;
57                 tree[a-1][c-1] = 1;
58                 isLeaves[a-1]= false;
59             }
60         }
61         BFS_version(tree, isLeaves);
62     }
63     return 0;
64 }
原文地址:https://www.cnblogs.com/bochen-sam/p/3351451.html