zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practise 1004 解题报告


    GitHub
    markdownPDF


    问题描述

    1. Counting Leaves (30)
      时间限制 400 ms
      内存限制 65536 kB
      代码长度限制 16000 B
      判题程序 Standard
      作者 CHEN, Yue
      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 ID1 ID2 ... 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

    大意是:

    对于一个树,输出每层节点中叶子节点的个数。


    解题思路

    链表, 或者用搜索也行。这里只举出链表的做法。

    链表可以用一维数组,也能用结构体实现。这里(因为懒)用了数组的方式。


    代码

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int main()
    {
    	//freopen("in.txt","r",stdin);
    	int i,j,k,n,m,s,t,a[101]={0},b[101]={0},c[102]={0};
    	cin>>n>>m;
    	for (i=0;i<m;i++)
    	{
    		cin>>s>>t;
    		b[s]=1;
    		for (j=0;j<t;j++)
    		{
    			cin>>k;
    			a[k]=s;
    		}
    	}
    	s=0;
    	for (i=1;i<=n;i++)
    	if (b[i]==0)
    	{
    		t=a[i];
    		k=1;
    		while (t>0)
    		{
    			k++;
    			t=a[t];
    		}
    		c[k]++;
    		if (k>s) s=k;
    	}
    	cout<<c[1];
    	for (i=2;i<=s;i++) cout<<' '<<c[i];
    	return 0;
    } 
    

    提交记录

    此处输入图片的描述

  • 相关阅读:
    Python 从入门到进阶之路(一)
    Egg 企业级应用开发框架的搭建
    koa2 从入门到进阶之路 (七)
    koa2 从入门到进阶之路 (六)
    koa2 从入门到进阶之路 (五)
    衣服洗一个月之后失踪,这个互联网洗衣平台把衣服洗出了翅膀
    CSS3 Gradient 渐变还能这么玩
    MessageChannel 消息通道
    前端面试(算法篇)
    JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )
  • 原文地址:https://www.cnblogs.com/S031602240/p/6354111.html
Copyright © 2011-2022 走看看