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
解答:最近几天,我把树的存储实现看了一下,对于解此题还是相当有帮助的。树可以用双亲表示法、孩子节点表示法、孩子兄弟节点表示法等。本题我用了孩子节点表示法,用孩子链表来存储。孩子链表和图的邻接表存储结构非常相似,毕竟树是图的一种特殊形式。
思路:就讲一下方法(1)吧。先将数据存储起来,然后用树的层次遍历(对应于图的广度优先遍历),对每层的节点进行判定,如果没有孩子,那么sum++。此题我的解法中还需要保存每一层中的节点数量,这样才能输出每一层的情况。
在下面方法(1)和(2)都是用邻接表的存储实现的,(1)是自己实现,比较花时间,但对于个人编程能力提升还是相当有帮助的,方法(2)用的是STL库中的vector,vector<vector<int> >的类型能够很好的模拟邻接表的实现,对于遍历,我使用了DFS。
(1)AC代码(用邻接表实现)如下:
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;
typedef struct ArcNode{
int id;
struct ArcNode* nxt;
}ArcNode;
typedef struct VNode{
int id;
int childs;
ArcNode* firstchild;
}VNode, ArcList[105];
typedef struct{
int vernum;
ArcList list;
}Graph;
Graph graph;
int first = 1;
ArcNode* getArcNode(int cid)
{
ArcNode* nodeptr = (ArcNode*)malloc(sizeof(ArcNode));
if(nodeptr == NULL){ printf("内存分配失败!\n"); exit(1); }
nodeptr->id = cid;
nodeptr->nxt = NULL;
return nodeptr;
}
void InsertArcNode(int id, int cid)
{
ArcNode* ptr = graph.list[id].firstchild;
while(ptr->nxt != NULL)
{
ptr = ptr->nxt;
}
ptr->nxt = getArcNode(cid);
}
queue<int> que; int count = 0; int sum = 0;
int BFS()
{
que.push(1); int level1 = 1, level2 = 0;
while(!que.empty())
{
int id = que.front(); que.pop(); level1--;
//cout <<"::"<< id << "::";
if(graph.list[id].firstchild == NULL) sum++;
for(ArcNode* ptr = graph.list[id].firstchild; ptr != NULL; ptr = ptr->nxt)
{
que.push(ptr->id); level2++;
}
if(level1 == 0)
{
if(first) { first = 0; printf("%d", sum);}
else printf(" %d", sum);
level1 = level2; level2 = 0; sum = 0;
}
}
}
int main()
{
int M;
scanf("%d %d", &graph.vernum, &M);
//对graph进行初始化
for(int i = 1; i <= graph.vernum; ++i)
{
graph.list[i].id = i;
graph.list[i].firstchild = NULL;
graph.list[i].childs = 0;
}
while(M--)
{
int id, childnum;
scanf("%d %d", &id, &childnum);
graph.list[id].childs = childnum;
while(childnum--)
{
int cid;
scanf("%d", &cid);
if(graph.list[id].firstchild == NULL)
{
graph.list[id].firstchild = getArcNode(cid);
}
else
{
InsertArcNode(id, cid);
}
}
}
//输出看看
/*for(int i = 1; i <= graph.vernum; ++i)
{
cout << i << " ";
for(ArcNode* ptr = graph.list[i].firstchild; ptr != NULL; ptr = ptr->nxt)
{
cout << ptr->id << " ";
}
cout << endl;
}*/
BFS();
return 0;
}
(2)AC代码(用vector<vector<int> >实现)代码如下,可见,和方法(1)比较,方法(2)简洁直观,所以说要好好利用轮子:
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
vector<vector<int> > vec(100); //这样就不用自己去建邻接表了
int book[100];
int maxdepth = -1;
void dfs(int id, int depth)
{
if(vec[id].size() == 0)
{
book[depth]++;
maxdepth = max(maxdepth, depth);
}
for(int i = 0; i < vec[id].size(); ++i)
{
dfs(vec[id][i], depth+1);
}
}
int main()
{
int N, M;
memset(book, 0, sizeof(book));
scanf("%d %d", &N, &M);
while(M--)
{
int id, n;
scanf("%d %d", &id, &n);
while(n--)
{
int cid;
scanf("%d", &cid);
vec[id].push_back(cid);
}
}
dfs(1, 0);
int first = 1;
for(int i = 0; i <= maxdepth; ++i)
{
if(first){
first = 0; cout << book[i];
}else{
cout << " " << book[i];
}
}
return 0;
}