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
题目大意:给你一个图,判断这个图能不能构成树,不能构成输出它的连通分量。能构成树,输出以哪些节点为根树的高度最高。
首先通过dfs寻找连通分量的个数,如果不为1,输出无法构成树。
如果为1:两遍dfs找最高的点:
首先以某个点为根,进行dfs(),得到高度最高的点。再从这些点中随机选择一个点再进行dfs,保存高度最高的点,两次遍历的并集即为答案。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<set>
using namespace std;
vector<int>v[];
bool vis[];
vector<int>temp;
set<int>s;
int maxheight;
void dfs(int n,int depth)
{
if(maxheight<depth)
{
maxheight=depth;
temp.clear();
temp.push_back(n);
}
else if(depth==maxheight)
{
temp.push_back(n);
}
vis[n]=;
for(int i=;i<v[n].size();i++)
{
if(!vis[v[n][i]])
{
dfs(v[n][i],depth+);
}
}
}
int main()
{
int n,a,b;
scanf("%d",&n);
memset(vis,,sizeof(vis));
for(int i=;i<=n-;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
int cnt=,s1;
for(int i=;i<=n;i++)
{
maxheight=;
if(!vis[i])
{
dfs(i,);
for(int j=;j<temp.size();j++)
{
cout<<temp[j]<<endl;
s.insert(temp[j]);
if(j==)
s1=temp[j];
}
cnt++;
}
}
if(cnt!=)
printf("Error: %d components\n",cnt);
else
{
memset(vis,,sizeof(vis));
dfs(s1,);
for(int i=;i<temp.size();i++)
s.insert(temp[i]);
set<int>:: iterator it;
it=s.begin();
for(it;it!=s.end();it++)
printf("%d\n",*it);
}
}

PAT1021(dfs 连通分量)的更多相关文章

  1. 【dfs+连通分量】Bzoj1123 POI2008 BLO

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  2. 图论算法之DFS与BFS

    概述(总) DFS是算法中图论部分中最基本的算法之一.对于算法入门者而言,这是一个必须掌握的基本算法.它的算法思想可以运用在很多地方,利用它可以解决很多实际问题,但是深入掌握其原理是我们灵活运用它的关 ...

  3. Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】

    题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...

  4. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

  5. leetcode-200-岛屿的个数(dfs找所有的连通分量)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  6. Graph_Master(连通分量_D_Trajan缩点+dfs)

    hdu_2242 题目大意:求将一张无向图(n个点,m条边)移除一条边分为不连通两部分,使得两部分的点权和最接近,若无法分为两部分,则输出impossible. 题解:拿到题面还算清晰,就是先tarj ...

  7. SDUT OJ 之 连通分量个数 (dfs)

    数据结构实验:连通分量个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...

  8. 数据结构之 图论---连通分量的个数(dfs搜索)

    数据结构实验:连通分量个数 Time Limit: 1000MS Memory limit: 65536K 题目描述  在无向图中,如果从顶点vi到顶点vj有路径,则称vi和vj连通.如果图中任意两个 ...

  9. 搜索(DFS)---好友关系的连通分量数目

    好友关系的连通分量数目 547. Friend Circles (Medium) Input: [[1,1,0], [1,1,0], [0,0,1]] Output: 2 Explanation:Th ...

随机推荐

  1. leetcode988

    public class Solution { private Stack<string> ST = new Stack<string>(); private string S ...

  2. Linux下部署URL重写

    在将项目部署在Linux服务器后可以通过URL重写隐藏应用的入口文件index.php cd /etc/apache2 vi apache2.conf 修改以下内容 将none改成Alll 重启服务 ...

  3. js 深度拷贝

    js 数据类型 分为2种: 基本数据类型:Undefined.Null.Boolean.Number.String 复杂数据类型:Object.Array.function 他们的区别是在内存中的存储 ...

  4. react-native android app名字 app包名、图标和启动图片设置

    1.设置名字 打开 android/app/src/main/res/values/strings.xml 如图,进行修改即可 2.设置图标,最简单可以直接替换,其他后在看 在上图中几个文件夹中都有一 ...

  5. Spring boot 启动配置原理

    配置在META-INF/spring.factories 有几个主要的类 ApplicationContextInitializer    创建SpringAplication SpringAppli ...

  6. django 之manytomany

    https://www.cnblogs.com/changbaishan/p/8056762.html https://blog.csdn.net/hpu_yly_bj/article/details ...

  7. vue.js 组件引用之初级 之二

    1. template 标签也可以实现替换,这样可以省去script标签了 <!DOCTYPE html> <html lang="en"> <hea ...

  8. C++指针理解

    指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...

  9. Haskell语言学习笔记(89)Unicode UTF8

    unicode-show $ cabal install unicode-show Installed unicode-show-0.1.0.2 Prelude> :m +Text.Show.U ...

  10. RMI 、RPC和SOAP