PAT Advanced 1021. Deepest Root (25) (C語言實現(xiàn))

我的PAT系列文章更新重心已移至Github屹逛,歡迎來看PAT題解的小伙伴請到Github Pages瀏覽最新內(nèi)容(本篇文章鏈接)深啤。此處文章目前已更新至與Github Pages同步拗馒。歡迎star我的repo

題目

A graph which is connected and acyclic can be considered a tree. The height of
the tree depends on the selected root. Now you are supposed to find the root
that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains
a positive integer N ( \le 10^4 ) which is the number of nodes, and hence
the nodes are numbered from 1 to N . Then N-1 lines follow, each describes
an edge by given the two adjacent nodes' numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root
is not unique, print them in increasing order of their numbers. In case that
the given graph is not a tree, print Error: K components where K is the
number of connected components in the graph.

Sample Input 1:

5
1 2
1 3
1 4
2 5

Sample Output 1:

3
4
5

Sample Input 2:

5
1 3
1 4
2 5
3 4

Sample Output 2:

Error: 2 components

思路

這道題主要做的事情就是找到構(gòu)成的樹達到最大深度的根節(jié)點溯街,使用DFS即可诱桂。
題目還要求所給的圖不是樹的話洋丐,也就是說這個圖不是連通的,
這時候需要知道到底有幾個部分挥等,可以對剩下DFS未遍歷到的結(jié)點繼續(xù)DFS知道全部遍歷友绝,
過程中記數(shù)即可。

代碼

最新代碼@github肝劲,歡迎交流

#include <stdio.h>

typedef struct node{
    int visited, level, depth;
    struct adj *adj;
} node;

typedef struct adj{
    struct node *node;
    struct adj *next;
} adj;

void DFS(node *n, int level)
{
    n->visited = 1;
    n->level = level + 1;

    for(adj *a = n->adj; a; a = a->next)
        if(a->node->visited == 0)
            DFS(a->node, level + 1);
}

int main()
{
    int N, n1, n2, count, depth, maxdepth = 0;
    node nodes[10000] = {0}, *pnode;
    adj edges[20000] = {0}, *padj;

    /* Read and build the adjacent linked list */
    scanf("%d", &N);
    for(int i = 0; i < N - 1; i++)
    {
        scanf("%d %d", &n1, &n2);
        /* n1 to n2 */
        pnode = &nodes[n1 - 1];
        padj = &edges[i * 2];
        padj->node = &nodes[n2 - 1];
        padj->next = pnode->adj;
        pnode->adj = padj;
        /* n2 to n1 */
        pnode = &nodes[n2 - 1];
        padj = &edges[i * 2 + 1];
        padj->node = &nodes[n1 - 1];
        padj->next = pnode->adj;
        pnode->adj = padj;
    }

    for(int i = 0; i < N; i++)
    {
        /* Reset the whole graph */
        depth = 0;
        count = 1;
        for(int i = 0; i < N; i++)
            nodes[i].visited = nodes[i].level = 0;

        /* Start from the ith node */
        DFS(nodes + i, 0);

        /* Get the depth of the tree */
        for(int i = 0; i < N; i++)
            if(nodes[i].visited == 1)
                if(nodes[i].level > depth)
                    depth = nodes[i].level;

        /* Try to find other disjoint components */
        for(int i = 0; i < N; i++)
            if(nodes[i].visited == 0)
            {
                DFS(nodes + i, 0);
                count ++;
            }

        if(count != 1)  /* If not all the nodes are visited */
        {
            printf("Error: %d components", count);
            return 0;  /* Only have to do it once */
        }
        else            /* It is one tree */
        {
            nodes[i].depth = depth;
            if(maxdepth < depth)
                maxdepth = depth;
        }
    }

    /* Find the root with the same maximum depth */
    for(int i = 0; i < N; i++)
        if(nodes[i].depth == maxdepth)
            printf("%d\n", i + 1);

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末迁客,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子辞槐,更是在濱河造成了極大的恐慌掷漱,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件榄檬,死亡現(xiàn)場離奇詭異卜范,居然都是意外死亡,警方通過查閱死者的電腦和手機丙号,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門先朦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人犬缨,你說我怎么就攤上這事喳魏。” “怎么了怀薛?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵刺彩,是天一觀的道長。 經(jīng)常有香客問我枝恋,道長创倔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任焚碌,我火速辦了婚禮畦攘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘十电。我一直安慰自己知押,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布鹃骂。 她就那樣靜靜地躺著台盯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪畏线。 梳的紋絲不亂的頭發(fā)上静盅,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音寝殴,去河邊找鬼蒿叠。 笑死明垢,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的栈虚。 我是一名探鬼主播袖外,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼魂务!你這毒婦竟也來了曼验?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤粘姜,失蹤者是張志新(化名)和其女友劉穎鬓照,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孤紧,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡豺裆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了号显。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片臭猜。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖押蚤,靈堂內(nèi)的尸體忽然破棺而出蔑歌,到底是詐尸還是另有隱情,我是刑警寧澤揽碘,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布次屠,位于F島的核電站,受9級特大地震影響雳刺,放射性物質(zhì)發(fā)生泄漏劫灶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一掖桦、第九天 我趴在偏房一處隱蔽的房頂上張望本昏。 院中可真熱鬧,春花似錦枪汪、人聲如沸涌穆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至朱监,卻和暖如春岸啡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赫编。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工巡蘸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留奋隶,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓悦荒,卻偏偏與公主長得像唯欣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子搬味,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,457評論 0 13
  • 題目 Find the nth digit of the infinite integer sequence 1,...
    njim3閱讀 254評論 0 0
  • 有一種老師境氢,軟綿綿的,他總有本事讓原本歡脫的你碰纬,立刻開始覺得困萍聊、好困呀……好想睡覺。還有一種老師悦析,他精力旺盛寿桨,一開...
    懶YY的日本二三事閱讀 510評論 0 0
  • 原創(chuàng)美文/笨笨的布谷鳥 因為遇見便從此有了故事,因為時光便讓我們懂得了珍惜强戴。如水潺潺的光陰終阻擋不了歲月的腳步亭螟,我...
    笨笨的布谷鳥閱讀 707評論 0 1