PAT 甲级 1021 Deepest Root (25分)

题目

1021 Deepest Root (25分)

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 (≤ 1 0 4 10^4 104) 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

题目大意

连通且无环的图可以视为树,树的高度取决于所选的根结点,给出结点数量N以及N-1条边,找到使得树的深度最大的结点,即“最深的根”。

思路

方法一
利用DFS遍历可以得到连通分量的个数,如果不等于1,则报错;如果等于1,在每次从一个点开始DFS遍历的时候可以得到深度最大的结点的集合,先从结点1得到最初的深度最大的结点集合,然后从其中选择一个再次使用DFS,则得到另一个结点集合,两者的并集就是所求结果。
方法二
在不知道方法一的情况下,直接对所有结点DFS,可以得到每个结点的最大深度,根据最大深度遍历表示各个结点最大深度的数组,输出结果。

代码

// 方法一
//#include<bits/stdc++.h>
//using namespace std;
//vector<vector<int> > edge;
//vector<bool> visit;
//vector<int> nodes;
//set<int> s;
//int maxheight = 0;
//void dfs(int v, int height){
//    visit[v] = true;
//    if(height > maxheight){
//        maxheight = height;
//        nodes.clear();
//        nodes.push_back(v);
//    }else if(height == maxheight)
//        nodes.push_back(v);
//
//    for(auto it : edge[v])
//        if(!visit[it])
//            dfs(it, height + 1);
//}
//int main(int argc, const char * argv[]) {
//    int N, a, b, ans=0, x;
//    cin>>N;
//    edge.resize(N+1);
//    visit.resize(N+1, false);
//    for(int i=0; i<N-1; i++){
//        cin>>a>>b;
//        edge[a].push_back(b);
//        edge[b].push_back(a);
//    }
//    for(int i=1; i<=N; i++){
//        if(!visit[i]){
//            dfs(i, 1);
//            ans++;
//            if(i == 1){
//                if(nodes.size() != 0) x = nodes[0];
//                for(auto it : nodes){
//                    s.insert(it);
//                }
//            }
//        }
//    }
//    if(ans != 1) printf("Error: %d components", ans);
//    else{
//        nodes.clear();
//        maxheight = 0;
//        fill(visit.begin(), visit.end(), false);
//        dfs(x, 1);
//        for(auto it : nodes)
//            s.insert(it);
//        for(auto it : s)
//            printf("%d\n", it);
//    }
//    return 0;
//}

#include<bits/stdc++.h>
using namespace std;
int maxheight = 0;
vector<int> depth;
vector<vector<int>> edge;
vector<bool> visit;
void dfs(int v, int height){
    visit[v] = true;
    maxheight = max(maxheight, height);
    depth[v] = max(depth[v], height);
    for(auto it : edge[v]){
        if(!visit[it]){
            dfs(it, height+1);
        }
    }
}
int main(){
    int N, a, b, ans = 0;
    cin>>N;
    edge.resize(N+1);
    for(int i=0;i<N-1; i++){
        cin>>a>>b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    depth.resize(N+1, 0);
    visit.resize(N+1, false);
    for(int i=1; i<=N; i++){
        if(!visit[i]){
            dfs(i, 0);
            ans++;
        }
    }
    if(ans != 1) printf("Error: %d components\n", ans);
    else{
        for(int i=1; i<=N; i++){
            fill(visit.begin(), visit.end(), false);
            dfs(i, 0);
        }
        for(int i=1; i<=N; i++)
            if(depth[i] == maxheight)
                printf("%d\n", i);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值