国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT A 1004. Counting Leaves (30)

2019-11-08 01:42:58
字體:
來源:轉載
供稿:網友
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 101 1 02Sample Output
0 1
//題目分析:
用vector存儲數量可變的子節點;
最后要求按層輸出葉子結點的個數;
vector assign 方法:vector<int> v; v.assign(n,t);//==>將n個t重新賦值給vector,可用于初始化
//代碼
#include <iostream>#include <vector>using namespace std;struct Node{	int id;	int level;	int k;	vector<int> child;};Node nodes[100];int maxLevel=0;vector<int> levels;void setLevel(int id,int level){	nodes[id].level=level;	if(maxLevel<level)		maxLevel=level;	if(nodes[id].child.empty()){		return;	}	for(int i=0;i<nodes[id].k;i++){		setLevel(nodes[id].child[i],level+1);	}	return;}void getLevels(int id){	if(nodes[id].child.empty()){		levels[nodes[id].level]++;		return;	}	for(int i=0;i<nodes[id].k;i++){		getLevels(nodes[id].child[i]);	}	return;}int main(){	int N,M;	cin>>N>>M;	for(int i=0;i<M;i++){		int K,id;		scanf("%d%d",&id,&K);		nodes[id].id=id;		nodes[id].k=K;		nodes[id].child.resize(K);		for(int k=0;k<K;k++){			scanf("%d",&nodes[id].child[k]);		}	}	setLevel(1,0);	levels.assign(maxLevel+1,0);	getLevels(1);	for(int i=0;i<levels.size()-1;i++){		cout<<levels[i]<<' ';	}	cout<<levels[levels.size()-1]<<endl;;	system("pause");	return 0;}//結果

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴彦县| 米易县| 滨州市| 吉首市| 巴里| 盘锦市| 曲麻莱县| 罗定市| 泽州县| 阜阳市| 竹溪县| 留坝县| 黔东| 三穗县| 隆昌县| 清丰县| 盐源县| 宜州市| 阜宁县| 个旧市| 南陵县| 永平县| 唐山市| 梧州市| 花莲县| 建宁县| 佛学| 民权县| 吴江市| 民权县| 谷城县| 平度市| 高要市| 东方市| 内乡县| 读书| 宜昌市| 会泽县| 阿拉善右旗| 林周县| 西乌|