A1004 Counting Leaves (30 分)

1004 Counting Leaves (30 ) 一、题目 大意:输入节点个数n和非叶节点个数m,并在接下来的m行中以如下形式输入节点id的子节点:id 子节点个数 子节点1 子节点2……。要求输出每一层中叶节点(没有孩子的节点)的个数。 二、有问题的思路和代码 #include <iostream> using namespace std; #define MaxNum 101 typedef struct Node{ ... 阅读详情

1004 Counting Leaves (30 分)

解题思路

       题目以家谱树为背景,真实的目的是以层为单位,计算叶子结点的数目。虽然考察的是树的相关内容,但是不需要建树。只需要借用层次遍历的思路就可以确定每个结点所属的层。然后使用哈希表在输入的时候标记非叶结点,剩下的就是叶子结点。

AC代码

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

vector<int> adj[101];
int hashtable[101] = {0};
int pos[101] = {0}; // 记录下标对应的结点的层
vector<int> output; // 记录下标对应的层的叶子结点的数目
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
    output.resize(n + 1);
    for (int i = 0; i < m; ++i)
    {
    	int id, k;
    	scanf("%d %d", &id, &k);
    	hashtable[id] = 1;
    	for (int i = 0; i < k; ++i) {
    		int temp;
    		scanf("%d", &temp);
    		adj[id].push_back(temp);
    	}
    }
    pos[1] = 1;
    queue<int> q;
    q.push(1);
    int level = 0;
    while(!q.empty()) {
    	int p = q.front();
    	q.pop();
    	for (int i = 0; i < adj[p].size(); ++i) {
    		q.push(adj[p][i]);
    		pos[adj[p][i]] = pos[p] + 1;
    		if (level < pos[p] + 1) 
    			level = pos[p] + 1;
    		if (hashtable[adj[p][i]] == 0)
    			output[pos[adj[p][i]]]++;
    	}
    }
    if (n == 1) printf("1\n"); // 特判,如果只有一个结点
    else if (n != 1 && n != 0){
    	printf("0 ");
    	for (int i = 2; i <= level; ++i) {
    		printf("%d%s", output[i], i == level ? "\n" : " ");
    	}
    }
    return 0;
}

[PAT] 1004 Counting Leaves30 )Java A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case s... 阅读详情

相关推荐

PAT甲级 1004.Counting Leaves (30) 题目翻译与答案

1004.计算叶子个数 一个家庭的层级结构经常被表现为一个家谱。你的任务是统计这些家庭成员中谁没有孩子。 输入 每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指中的结点个数(0<N<100),M指非叶结点的个数。然后下面有M行,每行的格式如下:

Coding home - 漂流瓶jz 8521

1004 Counting Leaves30

判断每层有多少个叶子节点 #include&lt;bits/stdc++.h&gt; using namespace std; const int maxn = 105; int max_h=1; int leaf[maxn]={0}; vector&lt;int&gt; G[maxn]; void DFS(int index, int h){ max_h = max(h, max_h); ...

姚军 341

1004Counting Leaves (30 )

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<algorithm> #include<map> #include<vector> #inclu...

发现问题,并解决问题,批判性思维 533

A1004 Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts...

iDestro的博客 265

PAT A1004 Counting Leaves (30)

PAT A1004 Counting Leaves 30原题大体题意思路代码运行结果 原题 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts with a lin

kyln1039的博客 172

PAT 甲级】A1004 Counting Leaves (30)

【版本一:层次遍历思想,22】 思想:开一个vector数组,记录每个节点的子节点;然后做一次层次遍历就好了。问题是如何区两个不同的层次呢?这里我引入了一个last变量来记录每一个层次最后一个节点的值。这样只需要不停地更新last就好啦~ 测试点一死活过不去,提示段错误!我要被折磨死了 #include <bits/stdc++.h> using namespace std; ve...

weixin_42278063的博客 215

PAT A1004 Counting Leaves (30 )

1004 Counting Leaves (30 ) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts with a line containing 0<N

一只小羊 936

A1004 Counting Leaves (30 )

#include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <vector> #include <queue> using namespace std; struct Node{ int layer; vect...

破烂仙人 129

PAT-A 1004 Counting Leaves (30 )

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts...

qq_38127801的博客 252

PAT A1004 Counting Leaves (30) C++/cpp实现

PAT A1004 Counting Leaves (30) 题目 原题链接 题目大意 按照题目要求输入节点信息,统计每一层时叶子节点的数量 大体思路 本质上就是构建一棵多叉,根据题目数据输入的特性,采用静态构建多叉,遍历访问每一个节点,判断该节点是不是叶子节点,如果是则相应的层数加1。该题可采用BFS和DFS,个人认为DFS写起来比较容易理解简洁。 代码 #include<stdio...

qq_41706836的博客 271

PAT甲级A1004Counting Leaves (30)(c++)

1004 Counting Leaves (30) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one t

小庄同学的博客 262

A1004 Counting Leaves30

#include&lt;cstdio&gt; #include&lt;vector&gt; using namespace std; const int maxn = 110; int n, m, k; vector&lt;int&gt; v[maxn]; int level[maxn] = { 0 }; int maxdepth = -1; void dfs(int root,int depth...

A_Aria的博客 190

PAT A 1004 Counting Leaves (30 )

题目概述&样例 即:族谱以形结构存储, 输出每层无子结点数量

Siumai's Blog 164
上一篇: A1003 Emergency (25 分)
下一篇: A1007 Maximum Subsequence Sum (25 分)
LinxRds
博客等级 码龄9年 0粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值