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. 转载:C++函数中new一块内存,作为返回值

    转载来自:http://blog.itpub.net/7728585/viewspace-2123621/ 今天遇到一个问题,C++编程时,函数中new一块内存,然后将申请内存的指针作为返回值.怎么d ...

  2. 将Oracle中的表结构导出到word

    语句如下: SELECT t1.Table_Name AS "表名称",t3.comments AS "表说明", t1.Column_Name AS &quo ...

  3. 【转】簡單講講 USB Human Interface Device

    原地址http://213style.blogspot.com/2013/09/usb-human-interface-device.html 恩,發本文的原因是看到了以前畢業的朋友在旁邊的對話框問了 ...

  4. microsoft visual c++ 14.0 is required问题解决办法

    方法有两个: 1.绕过pip,手动下载包 2.升级vc 详情参考:https://blog.csdn.net/amoscn/article/details/78215641

  5. php7内核执行流程(转载留记录)

  6. Redis脚本

    8.启动 /usr/local/bin/redis-server /etc/redis/redis.conf ./redis-server /home/work/redis/redis.conf &a ...

  7. js event

    event jquery获取: 在jquery中调用函数中最多只能有event这一个参数,所有的值在event.data中可以获取. $('select').click(function(event) ...

  8. svn下载地址

    SVN svn服务器端下载: https://www.visualsvn.com/server/download/ svn eclipse插件地址(new soft install): http:// ...

  9. 使用CommandLineRunner或ApplicationRunner接口创建bean

    在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...

  10. where后一个条件和多个条件的查询速度

    如果记录中有两个都是 唯一标识的  ,那是都where  and 还是只写一个比较快 ---- 一个快