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. leetcode1015

    class Solution(object): def smallestRepunitDivByK(self, K: int) -> int: if K % 2 == 0 or K % 5 == ...

  2. C# 类库调试 启动外部程序无法调试

    无法调试进程 test.exe [17936] 中的某些代码.请参阅下面的状态信息. IntelliTrace 代码失败(0x80131534).    Managed (v4.6.v4.5.v4.0 ...

  3. Notepadd ++ PluginManager安装

    下载地址https://github.com/bruderstein/nppPluginManager/releases 解压后有2个包plugins和updater 分别放入C:\Program F ...

  4. 伪AJAX

    <h3>3,伪ajax</h3> <h6>学习iframe(嵌套别人家网站的)</h6> <div> <input id=" ...

  5. iOS 证书, provision profile作用

    证书(certificate): 给app签名用的,针对开发者,app可以装在真机上的前提条件之一是被签名 Provision profile: 在app包中,用来校验app是否可以被装在真机上,一个 ...

  6. jquery接触初级----jquery 对象和Dom对象

    1. DOM 对象,每一份DOm对象(Document Object model)都可以表示成一棵树,一个基本的网页如下: <!DOCTYPE html> <html lang=&q ...

  7. 1037B--Reach Median(中位数)

    median 中位数 odd 奇数 even 奇数 You are given an array aa of nn integers and an integer ss. It is guarante ...

  8. 工厂模式——Head First

    这里主要谈论其中的工厂方法模式和抽象工厂模式. 一.定义 工厂方法模式(Factory Method Pattern)定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推 ...

  9. NIPS 2016上22篇论文的实现汇集

    http://blog.csdn.net/jiandanjinxin/article/details/54087592 日前,LightOn CEO 兼联合创始人 Igor Carron 在其博客上放 ...

  10. php面向对象 封装继承多态 接口、重载、抽象类、最终类总结

    1.面向对象 封装继承多态  接口.重载.抽象类.最终类 面向对象 封装继承多态  首先,在解释面向对象之前先解释下什么是面向对象? [面向对象]1.什么是类? 具有相同属性(特征)和方法(行为)的一 ...