题目如下:

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条边,属于稀疏图,应该用邻接矩阵而不是邻接表,否则会内存超限。

刚开始拿到这道题目的时候我想到的方法是用带颜色标记的BFS判断环路,并且排除掉,但是这样会超时,后来在网上查阅,看到大家都是使用并查集来判断是否是树,通过把所有元素归入并查集,并且在查询根的时候压缩路径,如果所有元素都是同根的,那么把他们加入Set后集合的规模为1,说明是一棵完整的树,如果不是如此,则说明图由多个连通的部分组成,连通部分的个数等于根的个数,因此只需要输出Set的规模即可。

如果是一棵树,只需要使用BFS来遍历树中的每一个顶点,来计算深度。

为了计算深度,改进BFS,加入三个变量:head、last、tail,last表示这一层的最后一个元素,tail表示下一层的某个元素,初始化时,让last=s,代表第一层只有源点,接下来在出队一个结点,并让head等于这个结点,然后在遍历结点的邻接点时不断的更新tail,不难看出tail在最后一次更新后指向的是下一层的最后一个元素,然后判断head是否等于last,如果是代表本层结束,把层计数变量+1,更新last为tail,以此类推即可。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <set> using namespace std; #define MAX 10002 bool visited[MAX];
vector<vector<int> > Graph;
int N;
int P[MAX]; void initSet(int N){ for(int i = 1; i <= N; i++) P[i] = i; } void CompressSet(int x, int top){ if(P[x] != top){
CompressSet(P[x],top);
P[x] = top;
} } int FindSet(int x){ if(x != P[x]){
int t = FindSet(P[x]);
CompressSet(x,t);
}
return P[x]; } void UnionSet(int x, int y){ int p1 = FindSet(x);
int p2 = FindSet(y);
P[p1] = p2; } int BFS(int s){ for(int i = 1; i <= N; i++) visited[i] = false;
int head,tail,last;
int stage = 0;
queue<int> Q;
Q.push(s);
visited[s] = 1;
last = s;
while(!Q.empty()){
int v = Q.front();
visited[v] = 1;
head = v;
Q.pop();
for(int index = 0; index < Graph[v].size(); index++){
int w = Graph[v][index];
if(!visited[w]){
Q.push(w);
tail = w;
}
}
if(head == last){
last = tail;
stage++;
} } return stage; } int main()
{
cin >> N;
int v1,v2; Graph.resize(N + 1);
initSet(N);
for(int i = 1; i <= N; i++){
visited[i] = false;
} for(int i = 1; i < N; i++){
scanf("%d%d",&v1,&v2);
Graph[v1].push_back(v2);
Graph[v2].push_back(v1);
} for(int i = 1; i <= N; i++){
for(int j = 0; j < Graph[i].size(); j++){
UnionSet(i,Graph[i][j]);
}
} set<int> root;
for(int i = 1; i<= N; i++){
root.insert(FindSet(i));
} if(root.size() != 1){
printf("Error: %d components\n",root.size());
return 0;
} vector<int> maxDeepNodes;
maxDeepNodes.clear();
int maxDepth = 0;
int depth = 0; for(int i = 1; i <= N; i++){
depth = BFS(i);
if(depth > maxDepth){
maxDepth = depth;
maxDeepNodes.clear();
maxDeepNodes.push_back(i);
}else if(depth == maxDepth){
maxDeepNodes.push_back(i);
}
} for(int i = 0; i < maxDeepNodes.size(); i++){
printf("%d\n",maxDeepNodes[i]);
} return 0;
}

1021. Deepest Root (25) -并查集判树 -BFS求深度的更多相关文章

  1. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  2. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  3. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  4. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  5. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  6. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

  7. 1021 Deepest Root (25)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  8. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  9. Is It A Tree? POJ - 1308(并查集判树)

    Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...

随机推荐

  1. python实现购物车

    一. 功能: 1. 用户充值余额 判断余额输入格式是否正确,正确则转换成float型. 2. 显示商品列表 根据已有商品显示所有商品的序号.商品名称.和价格供用户选择 3. 用户选择商品 判断用户输入 ...

  2. Android编译安装失败解决办法

    今天用AndroidStudio开发了一个手机App玩玩,但是偶然遇到一个问题,自己手机上测试得劲的很,分享给朋友做测试,但是nie,意外出现了.... 两个人都给我说个安装失败,这个就比较尴尬了,找 ...

  3. bash的工作特性及其使用方法

    bash的工作特性之命令执行状态返回值和命令展开所涉及的内容及其示例演出 !脚本执行与调试1.绝对路径执行,要求文件有执行权限2.以sh命令执行,不要求文件有执行权限3..加空格或source命令执行 ...

  4. vue--"卡片层叠" 组件 开发小记

    背景:影城移动点餐web App增加会员卡支付功能 需求:确认订单页点击会员卡项弹出会员卡列表,多张会员卡依次叠加覆盖上一张80%的高度,点击任意卡片则改卡片置为当前卡片,只有当前卡片显示全部卡片信息 ...

  5. 什么样的简历受HR青睐?

    简历是我们在求职过程中的名片,那么如何写出更容易受到HR青睐的简历呢? HR可能一天要看上百份的简历,他们都希望能够尽快筛选出合适的人,然后用更多的时间去跟候选人沟通.所以招聘人员一般看一份简历只会花 ...

  6. VUE相关资料合集

    ===官方=== https://github.com/vuejs/vue vue-components组件库 ---PC端--- https://github.com/ElemeFE/element ...

  7. Helm 架构 - 每天5分钟玩转 Docker 容器技术(161)

    在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...

  8. SpringMVC之Ajax与Controller交互

    前面学习了拦截器,通过拦截器我们可以拦截请求,做进一步处理之后再往下进行,这里我们使用Ajax的时候会有一个问题就是会把js.css这些静态资源文件也进行了拦截,这样在jsp中就无法引入的静态资源文件 ...

  9. 前端技术之_CSS详解第二天

    前端技术之_CSS详解第二天 1.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. & ...

  10. API得到Windows版本

    API得到Windows版本 /** * Windows Version * https://msdn.microsoft.com/en-us/library/windows/desktop/dn48 ...