PAT-A1021 Deepest Root【树】【树的深度】【DFS】

 

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.

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个节点。随后的n-1行,每行有两个数字,分别是相互连接的两个节点编号。

要求:

  • 如果可以构成一个树,那么就输出具有最大深度的根的编号
  • 不可以构成一棵树,根据格式输出连通分量

其实这道题还是典型,第一遍DFS染色法求连通分量,同时纪录取得最大深度的根节点。如果连通分量大于1,就输出结束了。否则在从上面的集合中随意选取一个点再做一次深搜,同样纪录第二次得到的节点,两次结果取交集


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#define maxN 10001
using namespace std;
bool marked[maxN];
vector<int> graph[maxN];
int maxHeight = 0;
vector<int> depthestNodes;
// 使用set,避免加入重复元素
set<int> points;
void dfs(int v, int height){
    marked[v] = true;
    if(height > maxHeight){
        depthestNodes.clear();
        depthestNodes.push_back(v);
        maxHeight = height;
    }else if(height == maxHeight){
        depthestNodes.push_back(v);
    }
    for(auto lt = graph[v].cbegin(); lt != graph[v].cend(); lt++){
        if(marked[*lt] == false){
            dfs(*lt, height + 1);
        }
    }
}
using namespace std;
int main() {
    int n, u, v;
    scanf("%d", &n);
    for(int i = 1; i < n; i++){
        scanf("%d %d", &u, &v);
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    fill(marked, marked + maxN, false);
    int count = 0;
    int s = 0;
    for(int i = 1; i <= n; i++){
        if(marked[i] == false){
            dfs(i, 1);
           
            count++;
        }
    }
    if(count > 1){
        printf("Error: %d components", count);
    }else{
        fill(marked, marked + maxN, false);
        maxHeight = 0;
        //将depthestNodes的第一个结点作为第二次dfs的起点(其实在depthestNodes任取一个都可以)
        // 为了重用depthestNodes,先将其中的所有元素加入到points中
        for(auto lt = depthestNodes.cbegin(); lt != depthestNodes.cend(); lt++){
            points.insert(*lt);
        }
        s = depthestNodes[0];
        
        // 第二次遍历
        // 记得清空depthestNodes
        depthestNodes.clear();
        dfs(s, 1);
        // 将第二次遍历的depthestNodes加入到points中
        for(auto lt = depthestNodes.cbegin(); lt != depthestNodes.cend(); lt++){
            points.insert(*lt);
        }
        
        // 输出结果
        for(auto lt = points.cbegin(); lt != points.cend(); lt++){
            printf("%d\n", *lt);
        }
    }
    
    return 0;
}

 

注意一点,虽然储存树可以用vector可以用二维数组表示,但是这道题的N太大(10000),如果使用int二维数组的话,会超出内存。

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值