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. PyCharm 安装使用

    服务器激活地址(转载)http://www.cnblogs.com/littlehb/p/7784517.html   PyCharm 服务器激活地址: 最近用edu邮箱申请了一个JetBrains针 ...

  2. springboot 前后端分离项目跨域配置

    @Configuration @EnableWebMvc public class CorsConfig implements WebMvcConfigurer { @Override public ...

  3. 获取数据库表中自增长最新的id

    mybatis <insert id="InsertCourse"> insert into training_course(type_id,course_title, ...

  4. LeetCode 题解 56. Merge Intervals

    题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...

  5. ORM查询api

    下面的方法都是对查询的结果进行出理:比如objects.filter.values()... 1)values(*field):返回一个可迭代的字典序列<QuerySet: [{name='小王 ...

  6. span标签 宽度无效解决方案

    完美的解决方案 下 面代码的CSS定义完美解决了span的宽度设置问题. 由于浏览器通常对不支持的CSS属性采取忽略处理的态度, 所以最好将display:inline -block行写在后面,这样在 ...

  7. UE 不生成.bak文件

    .bak文件是UE处理文件时自动备份的文件,可以取消备份这样就不会生成.bak文件了 菜单:高级-设置-文件处理-备份        应用和确定

  8. Linux下GDB调试简单示例

    这里介绍对文件first.c的基本GDB调试操作,只有部分命令,只是一个示例,运行环境为装有gcc编译器和gdb调试器的Linux环境,基本GDB调试命令如下表: 命令                 ...

  9. GDI+ 实现透明水印和文字

    最近给<JPEG浏览缩放器>增加了水印功能,在设计的过程中,参考了网上的文章,但是发现文章使用的GDI+ API封装包不是我现在使用的那一套,目前DELPHI使用的GDI+ API封装包有 ...

  10. 全局异常 同时ajax或是web跳转

    F8功能强大  在java代码debug的时候,F8键可直接跳到下一个类中.免去下一步 只用把之前两种方式合并即可,就是在exception包中不要ajax的异常,将其放入到web异常中,用if    ...