题目如下:

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. App上架应用市场,如何攻破安全过检难题

    App的安全过检与众所熟知的安全检测是两个完全不同的概念.首先App行业本身对App安全过检有一定的要求与规范,其次2017年6月1日正式实施的<中国网络安全法>中就曾要求App在渠道上线 ...

  2. sublime下配置C/C++运行环境

    最近在学习<WEB前端课程>老师教我们使用DW,但是不太喜欢,就选择了sublime,写前端代码还是很方便. 平时都是写C++,C比较多,借鉴了别人的配置步骤,将sublime打造成IDE ...

  3. SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...

  4. 地址下拉框,需要js级联js

    function area() { _url = "/ashx/DropDownControl.ashx"; _swType = "GetArea"; _z = ...

  5. MySQL/MariaDB 在插入数据的时候提示 Incorrect string value

    现象 今天开新工程,建表的时候弹出这玩意: what's this? 看起来好像是说我传入的内容不对? 可是仔细看看内容,没乱码,标准的中文字符串.想来是编码问题. 经过修改编码后,解决了问题. 解决 ...

  6. map函数用法详解

    map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...

  7. Webpack 2 设置为从当前文件夹逐级向上查找模块

    比较实用, 当你在cd到子文件夹运行webpack时,你可能想要require文件夹js里面的一些模块, 但你又想将祖先的js文件夹作为fallback.这样设置即可: module.exports ...

  8. Hadoop加速器GridGain

    GridGain的Hadoop加速器 像GridGain等内存网格产品(IMDG)不仅可以作为简单的缓存,加速Hadoop中MapReduce计算也是IMDG的一个亮点.这样内存计算领域又多了一种思路 ...

  9. Java中常用缓存Cache机制的实现

    缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例. 这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文件 ...

  10. rbac数据库设计

    1 rbac数据库设计 RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制分享牛原创,分享牛系列,分享牛.rbac 用户角色权限资源表如 ...