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 层级关系
  • 相关阅读:
    oracle11g 卸载和安装(win7,32位)
    MySQL忘记密码解决办法
    GPIO硬件资源的申请,内核空间和用户空间的数据交换,ioctl(.....),设备文件的自动创建
    模块参数,系统调用,字符设备编程重要数据结构,设备号的申请与注册,关于cdev的API
    开发环境的搭建,符合导出,打印优先级阈值
    定时器中断
    Linux系统移植的重要文件
    linux 相关指令
    linux各文件夹含义和作用
    外部中断实验
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9260911.html
Copyright © 2011-2022 走看看