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 层级关系
  • 相关阅读:
    在select标签中添加a标签
    js实现input的赋值
    JS截取字符串方法实例
    javascript中onclick(this)用法和onclick(this.value)用法介绍
    cookie的设置与销毁
    Js添加、读取、删除cookie,判断cookie是否有效,指定domain域下主路径path下设置cookie,设置expires过期时间
    自然语言处理的神经网络模型初探
    [Android] Toast问题深度剖析(二)
    如何使用 scikit-learn 为机器学习准备文本数据
    Android图像处理
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9260911.html
Copyright © 2011-2022 走看看