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 层级关系
  • 相关阅读:
    12.7 Test
    51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)
    BZOJ.3675.[APIO2014]序列分割(DP 斜率优化)
    BZOJ.4515.[SDOI2016]游戏(树链剖分 李超线段树)
    BZOJ.3165.[HEOI2013]Segment(李超线段树)
    Linux系统CentOS进入单用户模式和救援模式详解
    KVM 管理界面挂载多UKEY
    挂载银行前置机Ukey到windows server2012虚拟机的操作记录
    为什么服务器做了raid 系统文件还会丢失?
    LVS+Keepalived深度理解,阐述你不知道的坑点
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9260911.html
Copyright © 2011-2022 走看看