1021. Deepest Root

Deepest Root

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

题意

给定结点个数n以及 n - 1 条边。

首先判断这些结点是否构成树,即是否满足条件:所有结点连通,且不存在环。如果不是树,则输出结点构成的连通块的个数。

如果构成树,则输出结点,使得以该结点作为根结点能使树的高度最高,如果存在多个这样的结点,则按照从小到大顺序全部输出。

思路

判断是否是树,可以用并查集。如果最终得到的集合个数大于1,就说明不是树。并不需要去判断是否存在环,原因在于题目中给定了 n - 1 条边,如果只有一个连通块,那么就符合以下定理:满足连通、边数等于顶点数减1的结构一定是一颗树。如果存在环,那么 n - 1 条边至少会有两个连通块。

寻找最深根可以按照以下方法:

  1. 以任一结点i为根节点,找到离该结点最远的结点j,则结点j是最深根之一,若存在多个符合的结点,则构成一个集合A;
  2. 以第一步找到的集合A中的任一结点为根节点,搜寻所有离该结点最远的结点,构成集合B;
  3. 集合A和集合B中所有结点即为所求最深根。

该方法正确性的具体证明可参考 《pat1021Deepest Root (25)》


代码实现

#include <cstdio>
#include <vector>
#include <set>
#include <cstring>
using namespace std;

const int maxn = 100001;

int father[maxn];                   // 并查集父结点数组
bool root[maxn] = {false};          // 记录结点i是否是根节点
bool visit[maxn] = {false};         // DFS中结点i是否已访问
vector<int> adj[maxn];              // 邻接表
int maxLevel = 0;                   // 记录最大深度
set<int> ans, temp;                 // ans记录最终根结点集合,temp记录单次DFS中根结点集合

/* 父结点数组初始化 */
void initialize(int n)
{
    for (int i = 1; i <= n; i++)
        father[i] = i;
}

/* 寻找根结点,并压缩路径 */
int findRoot(int x)
{
    if (x == father[x])
        return x;

    int root = findRoot(father[x]);
    father[x] = root;       // 压缩路径
    return root;
}

/* 合并集合 */
void unite(int a, int b)
{
    int rootA = findRoot(a);
    int rootB = findRoot(b);
    if (rootA != rootB)         // 分属不同集合则合并
        father[rootA] = rootB;
}

void DFS(int v, int level)      // v为当前访问结点,level为当前结点深度
{
    bool flag = true;       // 判断与当前结点相连的所有结点是否已经全部访问

    visit[v] = true;        // 标记当前结点已访问
    for (int i = 0; i < adj[v].size(); i++)
    {
        int id = adj[v][i];
        if (visit[id] == false)
        {
            flag = false;
            DFS(id, level + 1);
        }
    }
    visit[v] = false;       // 恢复当前结点为未访问


    if (flag)       // 所有邻接点已全部访问
    {
        if (level > maxLevel)
        {
            maxLevel = level;
            temp.clear();       // 清空根结点集合
            temp.insert(v);     // 插入新根结点
        }
        else if (level == maxLevel)
            temp.insert(v);     // 直接插入新根结点
    }
}

int main()
{
    int n;
    int a, b;
    int numRoot = 0;            // 记录集合个数
    set<int>::iterator it;

    scanf("%d", &n);
    initialize(n);
    for (int i = 0; i < n - 1; i++)
    {
        scanf("%d%d", &a, &b);
        unite(a, b);            // 合并集合
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 1; i <= n; i++)
    {
        int x = findRoot(i);
        if (!root[x])           // 属于新集合
        {
            root[x] = true;
            numRoot++;
        }
    }

    if (numRoot > 1)
        printf("Error: %d components", numRoot);
    else
    {
        DFS(1, 1);          // 第一次DFS遍历
        ans = temp;         // 将第一次遍历得到的结果存入ans
        it = ans.begin();
        DFS(*it, 1);        // 从集合中任选一个进行第二次DFS遍历
        for (it = temp.begin(); it != temp.end(); it++)         // 将第二次结果存入ans
            ans.insert(*it);
    }

    for (it = ans.begin(); it != ans.end(); it++)
        printf("%d\n", *it);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值