1. 程式人生 > 其它 >1021. Deepest Root (25)(圖的遍歷,dfs,連通分量的個數)

1021. Deepest Root (25)(圖的遍歷,dfs,連通分量的個數)

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 (<=10000) 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

生詞

英文 解釋
acyclic 非迴圈的

題目大意:

給出n個結點(1~n)之間的n條邊,問是否能構成一棵樹,如果不能構成則輸出它有的連通分量個數,如果能構成一棵樹,輸出能構成最深的樹的高度時,樹的根結點。如果有多個,按照從小到大輸出。

分析:

首先深度優先搜尋判斷它有幾個連通分量。如果有多個,那就輸出Error: x components,如果只有一個,就兩次深度優先搜尋,先從一個結點dfs後保留最高高度擁有的結點們,然後從這些結點中的其中任意一個開始dfs得到最高高度的結點們,這兩個結點集合的並集就是所求

原文連結:https://blog.csdn.net/liuchuo/article/details/52294178

題解

咱就是說,這段證明誰能看懂呢。。。(除了各位大佬





1.用set確實更方便:自動去重+自動排序!
2.並查集模板容易忘寫init函式。

#include <bits/stdc++.h>
using namespace std;
const int N=100010;
vector<int> G[N];

bool isRoot[N];
int father[N];
int findFather(int x)
{
    int a=x;
    while(x!=father[x]){
        x=father[x];
    }
    while(a!=father[a]){
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}
void Union(int a,int b)
{
    int faA=findFather(a);
    int faB=findFather(b);
    if(faA!=faB){
        father[faA]=faB;
    }
}
void init(int n)
{
    for(int i=1;i<=n;i++){
        father[i]=i;
    }
}
int calBlock(int n)
{
    int Block=0;
    for(int i=1;i<=n;i++){
        isRoot[findFather(i)]=true;
    }
    for(int i=1;i<=n;i++)
        Block+=isRoot[i];
    return Block;
}
int maxH=0;
set<int> temp,Ans;
void DFS(int u,int Height,int pre)
{
    if(Height>maxH){
        temp.clear();
        temp.insert(u);
        maxH=Height;
    }else if(Height==maxH){
        temp.insert(u);
    }
    for(int i=0;i<G[u].size();i++){
        if(G[u][i]==pre) continue;
        DFS(G[u][i],Height+1,u);
    }
}
int main() {
    int a,b,n;
    cin>>n;
    init(n);
    for(int i=1;i<n;i++){
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
        Union(a,b);
    }
    int Block=calBlock(n);
    if(Block!=1)
        cout<<"Error: "<<Block<<" components"<<endl;
    else{
        DFS(1,1,-1);
        Ans=temp;
        auto it=Ans.begin();
        DFS(*it,1,-1);
        for(auto it=temp.begin();it!=temp.end();it++){
            Ans.insert(*it);
        }
        for(auto it=Ans.begin();it!=Ans.end();it++)
            cout<<*it<<endl;
    }
	return 0;
}

本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15877247.html