PAT (Advanced Level) Practice 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 N N ( ≤ 10 ​ 4 ≤10 ​^4 104) which is the number of nodes, and hence the nodes are numbered from 1 to N N N. Then N − 1 N−1 N1 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条边,问他们能否形成一颗N个节点的树,如果能则从中选出节点作为树根,使得整棵树的高度最大,输出所有满足要求的可以作为树根的节点。

思路

有N个节点,边数为N-1的图只要连通就一定是树,所以只要判断是否连通即可,可以用dfs或者并查集解决。在确定是树后,还要找出所有“最深的根”。比较好想的方法是从每个节点开始进行一次dfs,就能找到所有的答案。但是效率较高的方法只需要两次dfs。
第一,称最长的路径为直径,那么任意两条直径一定相交。不然的话,假设有两条不相交的直径(像这样| |),在这两条路径上各选择一个点,由于这是个连通图,这两个点连通,把这两个点连起来(变成了这样H),这个H型的图可以组成4条路径,其中至少有一条比原来的两条直径中的某一条长,这与这条路径是直径矛盾,得证。同样也可以证明任意三条直径一定有一段公共路径或交于同一点,否则如果这三条路径两两相交但不交于同一点,同样也可以拼接出比原来的直径中的某一条更长的路径导致矛盾。这样就可以继续推出所有的直径一定交于同一点或有一段公共路径。
第二,从任意节点出发,得到的最深节点的集合A一定是所求根节点集合的一部分。假设从a节点出发到达b的路径最远,而b不是“最深的根”,假设一条直径是cd,设cd的在a是根节点时的公共祖先是o,则一定有ob>max(co, od),那么ob就可以和co或od组成一条长于cd的路径,这与cd是直径矛盾。
第三,从b出发得到的最深节点的集合B与A的并集就是答案。由于所有的直径一定有一段公共路径或者是一个公共点,那么这段公共路径就可以把所有的直径分成三部分,那么也就把所有的点也分成了三部分,分别设为R(公共路径)和S、T。而且S中的点满足到R的距离相同,T也一样。任取一点,dfs得到的最长路径的另一个端点的集合一定等于S或T,再从S或T中的一点开始dfs,就可以得到T或S,而S+T就是最终的答案。
这道题由于之前要用dfs判断是否连通,所以在寻找“最深的根节点”时只需要再dfs一次即可。

代码

#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

int maxH = 0;
bool vis[10010];
set<int> tmp, ans;
vector<int> g[10010];

void dfs(int u, int height) {
    vis[u] = true;

    if (height > maxH) {  // 如果获得了更大的树高
        tmp.clear();
        tmp.insert(u);
        maxH = height;
    } else if (height == maxH)
        tmp.insert(u);

    for (int i = 0; i < g[u].size(); ++i)
        if (!vis[g[u][i]]) dfs(g[u][i], height + 1);
}

int main() {
    int a, b, n;
    scanf("%d", &n);

    for (int i = 1; i < n; ++i) {
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }

    int block = 0;
    for (int i = 1; i <= n; ++i) {
        if (!vis[i]) {
            ++block;
            dfs(i, 1);
        }
    }

    if (block > 1) printf("Error: %d components\n", block);
    else {
        ans = tmp;
        fill_n(vis + 1, n, false);
        dfs(*tmp.begin(), 1);
        for (auto &e : tmp) ans.insert(e);
        for (auto &e: ans) printf("%d\n", e);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值