PAT甲級1021Deepest Root (25 分)

思路

首先dfs求圖中樹的個(gè)數(shù)七蜘,接著兩遍dfs求樹直徑上的端點(diǎn)链烈,最后結(jié)果為兩次dfs得到的端點(diǎn)的并集

題目描述

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 (≤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

AC代碼(C++)

#include <iostream>
#include <vector>
#include <set>
#define MAXN 10005
using namespace std;
vector<int>nodes[MAXN];
set<int>ans;
bool visited[MAXN];
int maxdepth = 0;
void dfs(int s, int d){
    if(d > maxdepth){
        maxdepth = d;
        ans.clear();
        ans.insert(s);
    }else if(maxdepth == d)ans.insert(s);
    visited[s] = true;
    for(int i = 0; i < nodes[s].size(); i++)
        if(!visited[nodes[s][i]])
            dfs(nodes[s][i], d + 1);
}
int main(int argc, const char * argv[]) {
    int n, n1, n2, cnt = 0;
    scanf("%d", &n);
    for(int i = 0; i < n - 1; i++){
        scanf("%d%d", &n1, &n2);
        nodes[n1].push_back(n2);
        nodes[n2].push_back(n1);
    }
    for(int i = 1; i <= n; i++)
        if(visited[i] == false){
            dfs(i, 0);
            cnt++;
        }
    if(cnt > 1){printf("Error: %d components\n", cnt);return 0;}
    fill(visited, visited + n + 1, 0);
    set<int>tep = ans;
    dfs(*ans.begin(), 0);
    for(auto it = tep.begin(); it != tep.end(); it++){
        ans.insert(*it);
    }
    for(auto it = ans.begin(); it != ans.end(); it++){
        printf("%d\n", *it);
    }
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驰后,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌爷光,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澎粟,死亡現(xiàn)場離奇詭異蛀序,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)活烙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門徐裸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人啸盏,你說我怎么就攤上這事重贺。” “怎么了回懦?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵檬姥,是天一觀的道長。 經(jīng)常有香客問我粉怕,道長健民,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任贫贝,我火速辦了婚禮秉犹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘稚晚。我一直安慰自己崇堵,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布客燕。 她就那樣靜靜地躺著鸳劳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪也搓。 梳的紋絲不亂的頭發(fā)上赏廓,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音傍妒,去河邊找鬼幔摸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛颤练,可吹牛的內(nèi)容都是我干的既忆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼患雇!你這毒婦竟也來了跃脊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤苛吱,失蹤者是張志新(化名)和其女友劉穎匾乓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體又谋,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拼缝,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了彰亥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咧七。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖任斋,靈堂內(nèi)的尸體忽然破棺而出继阻,到底是詐尸還是另有隱情,我是刑警寧澤废酷,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布瘟檩,位于F島的核電站,受9級特大地震影響澈蟆,放射性物質(zhì)發(fā)生泄漏墨辛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一趴俘、第九天 我趴在偏房一處隱蔽的房頂上張望睹簇。 院中可真熱鬧,春花似錦寥闪、人聲如沸太惠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凿渊。三九已至,卻和暖如春缚柳,著一層夾襖步出監(jiān)牢的瞬間埃脏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工喂击, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留剂癌,地道東北人淤翔。 一個(gè)月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓翰绊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子监嗜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

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