zoukankan      html  css  js  c++  java
  • 1004 Counting Leaves (30)(30 point(s))

    problem

    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
    

    tip

    一颗树,找到每层的叶子数量。

    anwser

    #include<bits/stdc++.h>
    
    #define INF 0x3f3f3f3f
    #define Max 101
    #define mp(x, y) std::make_pair(x, y)
    
    int N, M, level[Max], maxLevel = 0;
    std::vector<int > kids[Max];
    
    //typedef std::pair<int ,int> node
    
    int main(){
    //	freopen("test.txt", "r", stdin);
    	
    	memset(level, 0, sizeof(level));
    	
    	std::cin>>N>>M;
    	int fa, kidNum, kid;
    
    	for(int i = 0; i < M; i++){
    		std::cin>>fa>>kidNum;
    		for(int j = 0; j < kidNum; j++){
    			std::cin>>kid;
    			kids[fa].push_back(kid);
    //			std::cout<<kid<<" ";
    		}
    //		std::cout<<std::endl;
    	}
    	
    //	for(int i = 1; i <= N; i++) {
    //		if(i == 1)std::cout<< kids[i].size();
    //		else std::cout<<" "<<kids[i].size();
    //	}
    
    	std::queue<std::pair<int, int> > que;
    	que.push(mp(1,0));
    	while(!que.empty()){
    		std::pair<int, int> father = que.front(); que.pop();
    		if(kids[father.first].size() == 0) {
    //			std::cout<<father.first<<std::endl;
    			level[father.second] ++;
    			maxLevel = father.second;
    			continue;
    		}
    		for(int i = 0; i < kids[father.first].size(); i++){
    			que.push(mp(kids[father.first][i], father.second+1));
    		}
    	}
    	
    	for(int i = 0; i <= maxLevel; i++)
    	{
    		if (i == 0) std::cout<<level[i];
    		else std::cout<<" "<<level[i];
    	}
    	return 0;
    }
    
    /*
    4 2
    01 1 02
    02 2 03 04
    */
    

    experience

    英语单词:

    • hierarchy 层级关系
  • 相关阅读:
    命令模式(Command Pattern)
    外观模式(Facade Pattern)
    Hash函数的安全性
    单向散列函数
    装饰者模式(Decorator Pattern)
    尝试设计LFSR加密器,并用CAP4验证随机性
    对称密码-流密码
    组合模式(Composite Pattern)
    桥接模式(Bridge Pattern)
    适配器模式(Adapter Pattern)
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9260911.html
Copyright © 2011-2022 走看看