1021 Deepest Root (25 分)------C语言

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

        这道题考察了连通集以及DFS/BFS算法,我这里采用的是DFS,因为涉及到需要求树的最高高度(其实就是最长路径),那么最好是使用深度优先搜索,直接将一条路搜索完得到长度比较方便。

        连通集这里很简单了,这里主要说一下怎么求最长路径。首先一个完全联通且无环的图的最长路径一定是叶子结点到叶子结点,那么我们第一次用DFS算法,以结点1为起点找到了长度最大的叶子结点,但是结点1并不一定是叶子结点,所以这个长度并不是我们最终的最大长度。所以我们需要再利用得到的叶子结点用DFS算法进行回溯,重新去找到真正的最大长度,也就是叶子结点到叶子结点。

        这题我采用了set容器,因为在我们使用两次DFS算法寻找路径的过程中,肯定有重复的结点,而set容器不仅可以自动排序,还能保证不重复,非常好用。

//1021 Deepest Root (25 分)
#include<stdio.h>
#include<set>
#include<vector>
#include<map>
#include<string.h>
using namespace std;
vector <int> v;
set <int> s;
map <int, vector<int> > m; 
int N, MaxHeight=0;
void DFS(int x, int Height, int visit[]);

int main()
{
    scanf("%d", &N);
    int i, visit[N+1], cnt=0, start;
    for(i=0;i<N-1;i++){
        int a, b;
        scanf("%d%d", &a, &b);
        m[a].push_back(b);
        m[b].push_back(a);
    }
    for(i=1;i<=N;i++){
        visit[i] = 0;
    }
    for(i=1;i<=N;i++){
        if(visit[i]==0){
            DFS(i, 0, visit);
            cnt++;
        }
        if(i==1){
            for(int j=0;j<v.size();j++){
                if(j==0) start = v[j];
                s.insert(v[j]);
            }
        }
    }
    if(cnt==1){
        memset(visit, 0, sizeof(visit));
        v.clear();
        DFS(start, 0, visit);
        for(i=0;i<v.size();i++){
            s.insert(v[i]);
        }

        set <int>::iterator it;
        int flag = 1; 
        for(it=s.begin();it!=s.end();it++){
            if(!flag) printf("\n%d", *it);
            else{
                printf("%d", *it);
                flag = 0;
            } 
        }
    }
    else printf("Error: %d components", cnt);
    return 0;
}

void DFS(int x, int Height, int visit[])
{
    if(Height>MaxHeight){
        v.clear();
        v.push_back(x);
        MaxHeight = Height;
    }
    else if(Height==MaxHeight){
        v.push_back(x);
    }
    visit[x] = 1;
    for(int i=0;i<m[x].size();i++){
        if(visit[m[x][i]]==0){
            DFS(m[x][i], Height+1, visit);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值