1004 Counting Leaves

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

一个家庭的层级结构通常用家谱树来表示。你的任务是统计那些没有孩子的家庭成员。

输入格式:

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.

The input ends with N being 0. That case must NOT be processed.

每个输入文件包含一个测试用例。

每个测试用例以一行开始,包含一个整数N(0<N<100),表示树中节点的数量,以及一个整数M(<N),表示非叶节点的数量。

接下来的M行每行表示一个非叶节点,格式如下:

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

其中ID 是一个两位数,表示给定的非叶节点,

K是它的孩子数量,后面跟着它的孩子们的两位数ID

为了简化起见,我们将根节点的ID固定为01

输入以N为0结束,该情况下不应处理。

输出格式:

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.

样例表示仅有2个节点的树,其中01是根节点,02是它唯一的孩子。

因此,在根节点01层级上,没有叶节点;在下一层级上,有1个叶节点。然后我们应该输出0 1

1004.png

Sample Input:

2 1
01 1 02

Sample Output:

0 1

我的思路

#include<bits/stdc++.h>
using namespace std;
int main(){
    int N,M;
    cin>>N>>M;
    vector<vector<int>> path(N,vector<int>(2,0));
    //path[i][0]第i个结点层数
	//path[i][1]第i个结点是否有孩子 
	for(int i=0;i<M;i++){
		string farther;
		cin>>farther;
		int x=stoi(farther)-1;
		path[x][1]=1;
		int k;
		cin>>k;
		string son[k];
		for(int j=0;j<k;j++){
			cin>>son[j];
			int y=stoi(son[i])-1;
			path[y][0]=path[x][0]+1;
		}
	}
	//res[i]第i层没孩子家庭个数
	//下标从0开始 
	//未设置则为101
	vector<int> res(100,101);
	for(int i=0;i<N;i++){
		if(res[path[i][0]]==101){
			res[path[i][0]]=0; 
		}
		if(path[i][1]==0){
			res[i]+=1;
		}
	}
	printf("%d",res[0]);
	for(int i=1;i<100;i++){
		if(res[i]==101)break;
		printf(" %d",res[i]);
	}
}

得分:3

Solution

#include<bits/stdc++.h>
using namespace std;
vector<int> v[100];
int book[100], maxdepth = -1;
void dfs(int index, int depth) {
    if(v[index].size() == 0) {
        book[depth]++;
        maxdepth = max(maxdepth, depth);
        return ;
    }
    for(int i = 0; i < v[index].size(); i++)
        dfs(v[index][i], depth + 1);
}
int main() {
    int n, m, k, node, c;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d %d",&node, &k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &c);
            v[node].push_back(c);
        }
    }
    dfs(1, 0);
    printf("%d", book[0]);
    for(int i = 1; i <= maxdepth; i++)
        printf(" %d", book[i]);
    return 0;
}

很清楚的看到,这是基于vector的特性,将树存入了一个类邻接表。然后通过深度优先搜索找到叶节点和对应其层次,并在book数组中进行计数。思路清晰,方法易懂。

邻接表与深度优先搜索知识详见数据结构章节。

题解中的vector二维数组还有另外的形式:

  • 写法一 vector<int> a[2];

    vector<int> a[2];
    
    for (int i=0;i<10;i++) {
    	a[0].push_back(i);
    }
    
    for (int i=0;i<50;i++) {
    	a[1].push_back(i);
    }
    
    cout<<a[0].size()<<endl;
    cout<<a[1].size()<<endl;
    

    输出结果:

    6
    66
    
  • 写法二 vector<vector<int>> a

    vector<vector<int>> a;
    vector<int> b1;
    vector<int> b2;
    
    a.push_back(b1);
    a.push_back(b2);
    cout<<a.size()<<endl;
    

    输出结果:

    2
    
  • 写法三 vector<vector<int>> a(2, b)

    vector<int> b;
    vector<vector<int>> a(2, b);
    vector<int> b1;
    a.push_back(b1);
    cout<<a.size()<<endl;
    

    输出结果:

    3
    
  • 写法四 vector<vector<int>> a(2,vector<int>(1, 1))

    vector<vector<int>> a(2,vector<int>(2, 1))
    for(int i=0;i<a.size();i++){
        for(int j=0;j<a[i].size();j++){
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    

    输出结果:

    1 1 
    1 1