zoukankan      html  css  js  c++  java
  • PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目

    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

    题目分析

    已知每个节点的子节点,统计每层叶子节点数

    解题思路

    思路 01

    1. dfs深度优先遍历树,h记录当前节点所在层数,max_h记录最大层数,int left[n]记录每层叶子节点数

    思路 02

    1. bfs广度优先遍历树(借助queue),max_h记录最大层数,int left[n]记录每层叶子节点数,int h[n]记录每个节点所在层数(根节点初始化为h[1]=0)

    知识点

    1. 广度优先遍历树,使用int h[n]记录每个节点所在层数
    2. 深度优先遍历树,使用参数h记录当前节点所在层数

    Code

    Code 01(dfs)

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    vector<int> nds[101];
    struct node {
    	int data;
    	int depth=0;
    };
    int max_h,leaf[101];
    void dfs(int index,int h) {
    	max_h=max(h,max_h);//记录最大层数
    	if(nds[index].size()==0) { //叶子节点
    		leaf[h]++;
    		return;
    	}
    	for(int i=0; i<nds[index].size(); i++) {
    		dfs(nds[index][i],h+1);
    	}
    }
    int main(int argc, char * argv[]) {
    	int n,m,rid,cid,k;
    	scanf("%d %d",&n,&m);
    	for(int i=0; i<m; i++) {
    		scanf("%d %d",&rid,&k);
    		for(int j=0; j<k; j++) {
    			scanf("%d",&cid);
    			nds[rid].push_back(cid); 
    		}
    	}
    	dfs(1,0);
    	for(int i=0; i<=max_h; i++) {
    		if(i!=0)printf(" ");
    		printf("%d",leaf[i]);
    	}
    	return 0;
    }
    

    Code 02(bfs)

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    vector<int> nds[101];
    int leaf[101],h[101],max_h;
    void bfs(){
    	queue<int> q;
    	q.push(1);
    	while(!q.empty()){
    		int now = q.front();
    		q.pop();
    		max_h=max(max_h,h[now]);
    		if(nds[now].size()==0){
    			leaf[h[now]]++;
    		} else{
    			for(int i=0;i<nds[now].size();i++){
    				h[nds[now][i]]=h[now]+1;
    				q.push(nds[now][i]);
    			}
    		}
    	} 
    }
    int main(int argc, char * argv[]) {
    	int n,m,rid,cid,k;
    	scanf("%d %d",&n,&m);
    	for(int i=0; i<m; i++) {
    		scanf("%d %d",&rid,&k);
    		for(int j=0; j<k; j++) {
    			scanf("%d",&cid);
    			nds[rid].push_back(cid); 
    		}
    	}
    	h[1]=0;
    	bfs();
    	for(int i=0;i<=max_h;i++){
    		if(i!=0)printf(" ");
    		printf("%d",leaf[i]);
    	}
    	return 0;
    }
    

    Code 3(bfs)

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    const int maxn=101;
    vector<int> nds[maxn];
    int maxh, leaves[maxn];
    struct node{
    	int d, h; // id high
    };
    void bfs(int root) {
    	queue<node> qnds;
    	qnds.push({root,0});
    	while(!qnds.empty()) {
    		node now = qnds.front();
    		qnds.pop();
    		maxh=max(now.h,maxh);
    		if(nds[now.d].size()==0)leaves[now.h]++;
    		else
    			for(int i=0; i<nds[now.d].size(); i++) {
    				qnds.push({nds[now.d][i],now.h+1});
    			}
    	}
    }
    int main(int argc,char * argv[]) {
    	int n,m,id,k,cid;
    	scanf("%d%d",&n,&m);
    	for(int i=0; i<m; i++) {
    		scanf("%d%d",&id,&k);
    		for(int j=0; j<k; j++) {
    			scanf("%d", &cid);
    			nds[id].push_back(cid);
    		}
    	}
    	bfs(1);
    	printf("%d",leaves[0]);
    	for(int i=1; i<=maxh; i++) {
    		printf(" %d",leaves[i]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
    SPOJ GSS3 Can you answer these queries III ——线段树
    SPOJ GSS2 Can you answer these queries II ——线段树
    SPOJ GSS1 Can you answer these queries I ——线段树
    BZOJ 2178 圆的面积并 ——Simpson积分
    SPOJ CIRU The area of the union of circles ——Simpson积分
    HDU 1724 Ellipse ——Simpson积分
    HDU 1071 The area ——微积分
    HDU 4609 3-idiots ——FFT
    BZOJ 2194 快速傅立叶之二 ——FFT
  • 原文地址:https://www.cnblogs.com/houzm/p/12322276.html
Copyright © 2011-2022 走看看