A_1004

1004 Counting Leaves (30)
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

分析:最開始我是想用圖來保存數(shù)據(jù)肉拓,然后從頂點1開始進行圖的bfs县匠,bfs的同時計算每行葉子節(jié)點的個數(shù)汁政。實現(xiàn)起來比較復(fù)雜。看了柳婼大神的解題媚值,才想起來使用vector。兩種都是使用圖來保存數(shù)據(jù),但vector是可變長度的诗良。而且API較多,使用起來很方便鲁驶。

#include <bits/stdc++.h>
#define INF 999999999

using namespace std;
vector<int> matrix[101];
int record[101] = {0};
int deepest = -1;
void dfs(int index, int depth)
{
    if(matrix[index].size() == 0){
        record[depth]++;
        deepest = deepest>depth?deepest:depth;
        return;
    }
    for(int i = 0; i < matrix[index].size(); i++)
        dfs(matrix[index][i], depth+1);
}
int main()
{
    int n, m;       //樹的節(jié)點鉴裹,樹的非葉節(jié)點 
    cin >> n >> m;
    int id, nn, i, j, temp;
    for(i = 0; i < m; i++){
        cin >> id >> nn;
        for(j = 0; j < nn; j++){
            cin >> temp;
            matrix[id].push_back(temp);
        }
    }
    dfs(1, 0);
    cout << record[0];
    for(i = 1; i <= deepest; i++)
        cout << " " << record[i];
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市钥弯,隨后出現(xiàn)的幾起案子径荔,更是在濱河造成了極大的恐慌,老刑警劉巖脆霎,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件总处,死亡現(xiàn)場離奇詭異,居然都是意外死亡睛蛛,警方通過查閱死者的電腦和手機鹦马,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來忆肾,“玉大人荸频,你說我怎么就攤上這事】透裕” “怎么了旭从?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長场仲。 經(jīng)常有香客問我和悦,道長,這世上最難降的妖魔是什么渠缕? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任鸽素,我火速辦了婚禮,結(jié)果婚禮上亦鳞,老公的妹妹穿的比我還像新娘馍忽。我一直安慰自己棒坏,他們只是感情好,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布舵匾。 她就那樣靜靜地躺著俊抵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坐梯。 梳的紋絲不亂的頭發(fā)上徽诲,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機與錄音吵血,去河邊找鬼谎替。 笑死,一個胖子當著我的面吹牛蹋辅,可吹牛的內(nèi)容都是我干的钱贯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼侦另,長吁一口氣:“原來是場噩夢啊……” “哼秩命!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起褒傅,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弃锐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后殿托,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體霹菊,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年支竹,在試婚紗的時候發(fā)現(xiàn)自己被綠了旋廷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡礼搁,死狀恐怖饶碘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叹坦,我是刑警寧澤熊镣,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站募书,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏测蹲。R本人自食惡果不足惜莹捡,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望扣甲。 院中可真熱鬧篮赢,春花似錦齿椅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至寥茫,卻和暖如春遣蚀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纱耻。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工芭梯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人弄喘。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓玖喘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蘑志。 傳聞我的和親對象是個殘疾皇子累奈,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • 不管是山林中簡單素凈小茅廬,還是一處鄉(xiāng)村樸素無華民舍或是中外宏偉古建筑急但,還有現(xiàn)代化城市地標澎媒。它們都有自...
    太陽小匠閱讀 232評論 8 12
  • 輸了就是輸了 再怎么努力也沒有用了 輸給我自己 輸給時間
    老袁太太閱讀 123評論 0 0
  • 年三十的晚上,兒子早早的睡覺了羊始,客廳里公公婆婆還有老公在看電視旱幼,聽著春晚的開場,我摟著睡著的兒子突委,突然很想念沒結(jié)婚...
    風瑟沐夕閱讀 302評論 0 1