1004 Counting Leaves (30分)-map、vector

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<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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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

解题思路:

  1. 树的表示:由于题目中树不是二叉树,因此需要其他形式来表示树。这里采用map和vector容器实现树的表示,map关键字为根结点ID,第二项为vector容器,存储子结点的ID。
  2. 统计每层叶结点数量:将根的ID压入一个层次向量中,last表示当前层次最后(最右边)一个结点,将last初值设为树的根;接下来读取层次向量中第一个元素,将它所有子结点压入层次中向量,如果没有子结点,计数器cnt加1。如果第一个元素等于last,则说明这一层结点统计完毕,cnt压入输出向量中,然后清零,将last重新赋值为层次向量中最后一个元素,即指向下一层次最后一个元素。当初向量第一个元素,此时向量非空,则继续循环。

AC代码:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
	int N,M,K,i;
	map<string,vector<string> >tree;
	vector<string> q;
	vector<int> ans;	
	cin>>N>>M;
	for(i=0;i<M;i++){
		string a,b;
		cin>>a>>K;
		while(K--){
			cin>>b;
			tree[a].push_back(b);
		}	
	}
	q.push_back("01");
	string last="01",now;
	int cnt=0,j;
	for(i=0;i<q.size();i++){
		for(j=0;j<tree[q[i]].size();j++){
			now=tree[q[i]][j];
			q.push_back(now);	
		}
		if(!j)
			cnt++;
		if(q[i]==last){
			last=now;
			ans.push_back(cnt);
			cnt=0;	
		}	
	}
	for(i=0;i<ans.size()-1;i++)
		printf("%d ",ans[i]);
	printf("%d",ans[i]);
	return 0;
}

 

 

SQL Prompt是Red Gate Software公司开发的一款强大的SQL代码编辑和优化工具,主要面向数据库开发者和管理员。版本11.0.1.16766是一个更新版本,它提供了更高效、更便捷的SQL编写环境,旨在提升SQL代码的可读性、效率和一致性。这个安装包包含了所有必要的组件,用于在用户的计算机上安装SQL Prompt工具。 SQL Prompt的核心功能包括自动完成、智能提示、格式化和重构。自动完成功能能够帮助用户快速输入SQL语句,减少键入错误;智能提示则基于上下文提供可能的选项,加快编写速度;格式化功能允许用户按照自定义或预设的编码规范对SQL代码进行美化,提升代码的可读性;而重构工具则可以帮助用户优化代码结构,消除潜在问题。 在描述中提到的“代码格式化规则来源于网络”,指的是用户可以通过下载网络上的json文件来扩展或定制SQL Prompt的代码格式化规则。这些json文件包含了特定的格式设置,如缩进风格、空格使用、注释位置等。将这些文件复制到指定的目录(例如:C:\Users\用户名\AppData\Local\Red Gate\SQL Prompt 10\Styles)后,SQL Prompt会读取这些规则并应用到代码格式化过程中,使得用户可以根据个人偏好或团队规范调整代码样式。 以下几点请注意: 1. 经实测,此版本支持最新的Sql Server 2022版的SSMS21 2. 此安装包中不包括keygen,请自行解决
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值