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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

1004. Counting Leaves (30)

2019-11-11 04:09:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

http://blog.csdn.net/xkzju2010/article/details/46868273

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 1 01 1 02

Sample Output

0 1 1082/5000

Sample Input 2 1 01 1 02 Sample Output 0 1

又如: 這里寫圖片描述

#include<iostream>#include<queue>using namespace std;#define MAX 101 //常量的定義/*序號(hào),層數(shù),孩子個(gè)數(shù),孩子數(shù)組,其中序號(hào)可用node數(shù)組下標(biāo)來(lái)表示*/struct node{ int level; int k; int child[MAX];};int main(){ node tree[MAX]; int N,M;cin>>N>>M; for(int i=1;i<=N;i++){ //下標(biāo)從1開始,下標(biāo)為節(jié)點(diǎn)序號(hào) tree[i].level=0; tree[i].k=0; } for(i=0;i<M;i++){ int index;cin>>index;cin>>tree[index].k; for(int j=0;j<tree[index].k;j++){ cin>>tree[index].child[j]; //tree[tree[index].child[j]].level=tree[index].level+1;//不能簡(jiǎn)單的+1,因?yàn)槟悴恢垒斎氲捻樞颍枰匦聮呙枰槐椤? } } for(i=1;i<=N;i++){ for(int j=0;j<tree[i].k;j++){ tree[tree[i].child[j]].level=tree[i].level+1; } } queue<int> q; q.push(1);//01插入 int lev=0,cnt=0; //lev某層,cnt某層的葉子節(jié)點(diǎn)數(shù) while(!q.empty()){ //BFS廣度優(yōu)先用隊(duì)列 int u=q.front();q.pop(); int curlev=tree[u].level; if(curlev!=lev){ cout<<cnt<<" "; cnt=0; lev=curlev; } if(tree[u].k==0) cnt++; for(int i=0;i<tree[u].k;i++){ q.push(tree[u].child[i]); } } cout<<cnt<<endl; return 0;}

重新遍歷計(jì)算層數(shù)解析:孩子的level一定是父親的level+1,這個(gè)是顯然的。03 1 07 02 3 04 05 06 01 2 02 03 我第一行輸入的是3號(hào)結(jié)點(diǎn),第二行輸入的2號(hào)結(jié)點(diǎn),第三行輸入的是根結(jié)點(diǎn),根據(jù)這個(gè)輸入順序就不好判斷3到底是第幾層了。

隊(duì)列解析:剛開始,跟結(jié)點(diǎn)入隊(duì),就是1入隊(duì)。然后進(jìn)入while,判斷隊(duì)列是不是空,發(fā)現(xiàn)不是空,進(jìn)入循環(huán)體,然后隊(duì)列元素出隊(duì),返回u 這個(gè)u是樹里邊結(jié)點(diǎn)的編號(hào),u現(xiàn)在是1,就是根結(jié)點(diǎn)。curlev是當(dāng)前這個(gè)結(jié)點(diǎn)(根結(jié)點(diǎn))的層號(hào),tree[u].lev是u號(hào)節(jié)點(diǎn)的層號(hào),curlev和lev都是0,然后跳過(guò)if,執(zhí)行下面的東西。if(tree[u].k==0)是判斷當(dāng)前這個(gè)節(jié)點(diǎn)的孩子數(shù)量是不是0,如果是0就當(dāng)然是葉子節(jié)點(diǎn)了。這里因?yàn)楦?jié)點(diǎn)有兩個(gè)孩子2和3,所以不是葉子節(jié)點(diǎn),然后底下那個(gè)for循環(huán)是依次讓當(dāng)前節(jié)點(diǎn)的孩子入隊(duì),就是把根結(jié)點(diǎn)的所有孩子依次入隊(duì),現(xiàn)在隊(duì)列里是2和3。這樣第一遍循環(huán)就完了,再?gòu)念^開始。現(xiàn)在隊(duì)列的元素是2和3,隊(duì)列不為空,執(zhí)行下面的循環(huán)體。u=q.front()得到隊(duì)頭元素,u=2,是第02號(hào)結(jié)點(diǎn)。然后curlev=1,是02結(jié)點(diǎn)的層數(shù),這里if判斷curlev和lev不相等,不相等了,說(shuō)明遍歷到下一層了,這時(shí)候就執(zhí)行if底下的代碼,輸出cnt,就是上一層的葉子結(jié)點(diǎn)數(shù)量,是0,然后讓cnt=0,重新統(tǒng)計(jì)現(xiàn)在這層的葉子結(jié)點(diǎn),并讓lev=curlev,更新lev,代表遍歷到這層了。再下面的if是判斷u這個(gè)結(jié)點(diǎn)是不是葉子結(jié)點(diǎn),02號(hào)有3個(gè)孩子,不是葉子結(jié)點(diǎn),cnt不++。然后下面的for是把02號(hào)的3個(gè)孩子依次入隊(duì)。現(xiàn)在隊(duì)列的元素是3 4 5 6。第三次進(jìn)入while循環(huán),不為空,q.front()得到隊(duì)頭,是3號(hào)結(jié)點(diǎn),并出隊(duì)。判斷curlev和lev是不是相等的,此時(shí)兩個(gè)都是1,相等跳過(guò)if。判斷3是不是葉子結(jié)點(diǎn),因?yàn)?有一個(gè)孩子不是葉子結(jié)點(diǎn),然后把3號(hào)結(jié)點(diǎn)的所有孩子入隊(duì),就是把7入隊(duì)。此時(shí)隊(duì)列元素是4 5 6 7。然后循環(huán)再開始,隊(duì)列不空,繼續(xù)得到隊(duì)頭元素是4號(hào)結(jié)點(diǎn),q.pop()出隊(duì),curlev這時(shí)候是04號(hào)結(jié)點(diǎn)的層數(shù),是2,而lev是1,此時(shí)兩個(gè)不等,不相等就要打印結(jié)果了cnt=0,打印0,并把step更新,step=2了,然后判斷04號(hào)是不是葉子結(jié)點(diǎn),發(fā)現(xiàn)04號(hào)的k是0,說(shuō)明他是葉子結(jié)點(diǎn),cnt++,因?yàn)閗是0,所以下面的for就不執(zhí)行了,現(xiàn)在隊(duì)列的元素是5 6 7,然后這三個(gè)元素的執(zhí)行過(guò)程和4號(hào)結(jié)點(diǎn)是一樣的,略。最后隊(duì)列是空了,退出while循環(huán),底下有一句打印cnt,就是把最后一層的葉子結(jié)點(diǎn)數(shù)量打印出來(lái)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 个旧市| 肥东县| 枣阳市| 喀什市| 吕梁市| 年辖:市辖区| 任丘市| 湟中县| 石嘴山市| 黄龙县| 上蔡县| 沐川县| 黄浦区| 巩义市| 曲靖市| 临漳县| 清新县| 巴中市| 太谷县| 文化| 秦安县| 成都市| 德化县| 都安| 东平县| 交城县| 河南省| 潮州市| 三台县| 南江县| 自贡市| 英山县| 岳阳县| 青岛市| 贺州市| 莱州市| 舒兰市| 乐山市| 高台县| 顺义区| 米易县|