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

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

1004. Counting Leaves (30)

2019-11-11 04:50:53
字體:
供稿:網(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 //常量的定義/*序號,層數(shù),孩子個數(shù),孩子數(shù)組,其中序號可用node數(shù)組下標(biāo)來表示*/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)序號 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;//不能簡單的+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,這個是顯然的。03 1 07 02 3 04 05 06 01 2 02 03 我第一行輸入的是3號結(jié)點(diǎn),第二行輸入的2號結(jié)點(diǎn),第三行輸入的是根結(jié)點(diǎn),根據(jù)這個輸入順序就不好判斷3到底是第幾層了。

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昭平县| 富裕县| 北海市| 奈曼旗| 抚松县| 宁波市| 武邑县| 海伦市| 内黄县| 溧水县| 郸城县| 连云港市| 宜黄县| 正宁县| 韩城市| 宁阳县| 横峰县| 伊金霍洛旗| 兴和县| 米易县| 宁乡县| 奉新县| 攀枝花市| 台州市| 芒康县| 洛南县| 伽师县| 西丰县| 宁远县| 旬阳县| 淮南市| 晋江市| 凤翔县| 安顺市| 房产| 崇州市| 上蔡县| 新兴县| 淮阳县| 蒙自县| 体育|