1094 The Largest Generation (25 分)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a family member, K (>0) is the number of his/her children, followed by a sequence of two-digit ID's of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.

Sample Input:

23 13
21 1 23
01 4 03 02 04 05
03 3 06 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18

Sample Output:

9 4

分析

設(shè)置數(shù)組dep存儲每個depth下節(jié)點(diǎn)個數(shù)华糖,dfs遍歷一遍樹麦向,再在dep中找到最大值和所在位置即可

code

#include <iostream>
#include <vector>

using namespace std;

struct node{
    vector<int> child;
    int depth;
};

vector<node> v;
vector<int> dep;
void dfs(int root,int depth){
    v[root].depth = depth;
    dep[depth]+=1;
    if(v[root].child.size()==0){
        return ;
    }
    for(int i=0;i<v[root].child.size();i++){
        dfs(v[root].child[i],depth+1);
    }
}



int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    v.resize(n+1);
    dep.resize(n+1);
    int a,b,c;
    for(int i=0;i<m;i++){
        scanf("%d %d",&a,&b);
        for(int j=0;j<b;j++){
            scanf("%d",&c);
            v[a].child.push_back(c);
        }
    }
    dfs(1,1);
    int maxd=1,maxg=1;
    for(int i=1;i<n;i++){
        if(dep[i]>maxd){
            maxd = dep[i];
            maxg = i;
        }
    }
    printf("%d %d",maxd,maxg);
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市客叉,隨后出現(xiàn)的幾起案子诵竭,更是在濱河造成了極大的恐慌,老刑警劉巖兼搏,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卵慰,死亡現(xiàn)場離奇詭異,居然都是意外死亡向族,警方通過查閱死者的電腦和手機(jī)呵燕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來件相,“玉大人再扭,你說我怎么就攤上這事∫勾#” “怎么了泛范?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長紊撕。 經(jīng)常有香客問我罢荡,道長,這世上最難降的妖魔是什么对扶? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任区赵,我火速辦了婚禮,結(jié)果婚禮上浪南,老公的妹妹穿的比我還像新娘笼才。我一直安慰自己,他們只是感情好络凿,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布骡送。 她就那樣靜靜地躺著,像睡著了一般絮记。 火紅的嫁衣襯著肌膚如雪摔踱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天怨愤,我揣著相機(jī)與錄音派敷,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛篮愉,可吹牛的內(nèi)容都是我干的般眉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼潜支,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了柿汛?” 一聲冷哼從身側(cè)響起冗酿,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎络断,沒想到半個月后裁替,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡貌笨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年弱判,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锥惋。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡昌腰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出膀跌,到底是詐尸還是另有隱情遭商,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布捅伤,位于F島的核電站劫流,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏丛忆。R本人自食惡果不足惜祠汇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望熄诡。 院中可真熱鬧可很,春花似錦、人聲如沸粮彤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽导坟。三九已至屿良,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間惫周,已是汗流浹背尘惧。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留递递,地道東北人喷橙。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓啥么,卻偏偏與公主長得像,于是被迫代替她去往敵國和親贰逾。 傳聞我的和親對象是個殘疾皇子欠拾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

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