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

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

1004. Counting Leaves (30)

2019-11-08 18:38:16
字體:
來源:轉載
供稿:網友
A family hierarchy is usually PResented by a pedigree tree. Your job is to count those family members who have no child.InputEach 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.OutputFor 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 Input2 101 1 02Sample Output0 1
#include <iostream>#include <cstdio>#include <cstring>#define N 105struct node {	int child[N];	int k;}s[N];int que[N*N];void init() {	int i;	for (i = 0; i < N; i++)		s[i].k = 0;}int main() {	int n,m,i,j,l,r,count,k,t;	while (scanf("%d%d",&n,&m) != EOF) {		init();		for (t = 0; t < m; t++) {			scanf("%d",&i);			scanf("%d",&s[i].k);			for (j = 0; j < s[i].k; j++) {				scanf("%d",&s[i].child[j]);			}		}		que[0] = 1;//根節點的編號是1		k = 1;//k表示當前的節點的子節點(孩子節點)		//l		for (l = 0, r = 1,t = 0; l < k; r = k,t++) {			count = 0;			for (; l < r; l++) {				i = que[l];				if (!s[i].k) count++;				for (j = 0; j < s[i].k; j++) {					que[k++] = s[i].child[j];				}			}			if (t) printf(" %d",count);			else printf("%d",count);		}		printf("/n");	}	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙洋县| 湖北省| 三门县| 开江县| 晋中市| 饶平县| 昔阳县| 米泉市| 任丘市| 黔东| 健康| 阿勒泰市| 凤阳县| 尉氏县| 洛阳市| 五原县| 肥乡县| 喀什市| 大新县| 靖江市| 武威市| 衡山县| 永年县| 汝州市| 永仁县| 故城县| 遂昌县| 文昌市| 临沭县| 安宁市| 晋州市| 诸暨市| 平湖市| 简阳市| 迁西县| 东山县| 东兰县| 天水市| 泰州市| 盘锦市| 无棣县|