zoukankan      html  css  js  c++  java
  • PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分)

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

    Input Specification:

    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.

    Output Specification:

    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
    

    思路

    输出每个高度叶子节点的个数。

    深搜,搜到底后,使本高度的叶子节点数加一。

    需要注意的是,我被卡了一组数据,因为我判断的是叶子节点的方法为mp[i].size() == 1

    实际上,当根节点只有一个子节点时,也满足上述条件,因此需要特判一下。

    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    #include <map>
    using namespace std;
    
    int N, M;
    vector<int> mp[110];
    int num[110];
    int vis[110];
    int maxd;
    
    void dfs(int index, int deep){
    	if(index != 1 && mp[index].size() == 1){
    		num[deep]++;
    		maxd = max(deep, maxd);
    		return;
    	}
    		
    	for(int i = 0; i < mp[index].size(); i++){
    		if(!vis[mp[index][i]]){
    			vis[mp[index][i]] = 1;
    			dfs(mp[index][i], deep + 1);
    		}
    	}
    }
    
    
    int main(){
    	cin >> N >> M;
    	for(int i = 0; i < M; i++){
    		int id = 0, k = 0;
    		cin >> id >> k;
    		int t = 0;
    		for(int j = 0; j < k; j++){
    			cin >> t;
    			mp[t].push_back(id);
    			mp[id].push_back(t);
    		}
    	}
    	
    	vis[1] = 1;
    	dfs(1, 1);
    	
    	if(mp[1].size() == 0)	cout << "1" << endl;
    	
    	for(int i = 1; i <= maxd; i++){
    		if(i == maxd)
    			cout << num[i];
    		else
    			cout << num[i] << " ";
    	}
    //	for(int i = 0; i < 100; i++){
    //		if(!mp[i].size())	continue;
    //		cout << i << " ";
    //		for(int j = 0; j < mp[i].size(); j++){
    //			cout << mp[i][j] << " ";
    //		}
    //		cout << endl;
    //	}
    	return 0;
    }
    
  • 相关阅读:
    redis-cluster
    Mycat-主从结构的准备
    Mycat-多实例的搭建
    MSC添加shard节点
    MongoDB -MSC集群的部署
    MySQL知识-redis实例
    MySQL知识-MySQL不同版本多实例
    #Linux学习# 用户和用户组管理
    #Linux学习# 软件包管理
    #Linux学习# 文本编辑器vim
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12318967.html
Copyright © 2011-2022 走看看