PAT A1021 Deepest Root (25 分)

2 篇文章 0 订阅
  • 题目
    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.

  • 题目大意
    给定一个图,判断这个图是否为树,若为树,求出使得树高度最大的节点,否则不为树,输出最大连通图的个数
  • 解题方法
    第一:需要判断是否为树,对每个节点进行DFS,计算出最大连通图的个数。
    第二:需要求出使树的高度最大的节点,直接计算以每个节点为根节点的树的高度,并将其存储在对应的数组中。最后对这个数组处理即可

代码实现:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int N, num = 0;
const int MAX = 10001;                //最大节点数为100001,需要用邻接表
vector<int> G[MAX];
vector<int> res;
bool vis[MAX] = {false};
bool cmp(int a, int b){
    return a > b;
}
void DFS(int x){
    vis[x] = true;
    for(int i = 0; i < G[x].size(); i++){
        if(vis[G[x][i]] == false)
            DFS(G[x][i]);
    }
}
int MAX_Deep = 0;
int DFS1(int x, int deep){
    vis[x] = true;
    if(deep > MAX_Deep)
        MAX_Deep = deep;
    for(int i = 0; i < G[x].size(); i++){
        if(vis[G[x][i]] == false)
            DFS1(G[x][i], deep + 1);
    }
    return MAX_Deep;
}

void travel1(){
    int MAX_deep = 0;
    for(int i = 1; i <= N; i++){
        memset(vis, false, sizeof(vis));              //每次都需要初始vis数组都为false
        int deep = DFS1(i, 1);
        res[i] = deep;
        MAX_Deep = 0;
    }
}
void travel(){
    for(int i = 1; i <= N; i++){
        if(vis[i] == false){
            DFS(i);
            num++;
        }
    }
}
int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N - 1; i++){
        int node1, node2;
        scanf("%d%d", &node1, &node2);
        G[node1].push_back(node2);
        G[node2].push_back(node1);
    }
    travel();
    if(num > 1){
        printf("Error: %d components", num);
    } else {
        res.resize(N + 1);
        travel1();
        int max_deep = 0;
        for(int i = 1; i <= N; i++){
            if(res[i] > max_deep)
                max_deep = res[i];
        }
        for(int i = 1; i <= N; i++){
            if(res[i] == max_deep){
                printf("%d\n", i);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值