国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高青县| 郧西县| 资溪县| 长乐市| 会理县| 桦甸市| 惠安县| 汕尾市| 杭锦后旗| 清水县| 昌宁县| 洮南市| 邛崃市| 江华| 澳门| 铜川市| 邯郸市| 陕西省| 永仁县| 冕宁县| 华安县| 秀山| 平凉市| 日喀则市| 灌阳县| 桐乡市| 永安市| 茌平县| 乌什县| 葫芦岛市| 泗阳县| 疏附县| 浮山县| 鸡西市| 梁山县| 连南| 犍为县| 乌什县| 泽州县| 岢岚县| 宁陵县|