PAT1021(dfs 连通分量)
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 连通分量)的更多相关文章
- 【dfs+连通分量】Bzoj1123 POI2008 BLO
Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...
- 图论算法之DFS与BFS
概述(总) DFS是算法中图论部分中最基本的算法之一.对于算法入门者而言,这是一个必须掌握的基本算法.它的算法思想可以运用在很多地方,利用它可以解决很多实际问题,但是深入掌握其原理是我们灵活运用它的关 ...
- Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】
题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...
- DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)
一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...
- leetcode-200-岛屿的个数(dfs找所有的连通分量)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- Graph_Master(连通分量_D_Trajan缩点+dfs)
hdu_2242 题目大意:求将一张无向图(n个点,m条边)移除一条边分为不连通两部分,使得两部分的点权和最接近,若无法分为两部分,则输出impossible. 题解:拿到题面还算清晰,就是先tarj ...
- SDUT OJ 之 连通分量个数 (dfs)
数据结构实验:连通分量个数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...
- 数据结构之 图论---连通分量的个数(dfs搜索)
数据结构实验:连通分量个数 Time Limit: 1000MS Memory limit: 65536K 题目描述 在无向图中,如果从顶点vi到顶点vj有路径,则称vi和vj连通.如果图中任意两个 ...
- 搜索(DFS)---好友关系的连通分量数目
好友关系的连通分量数目 547. Friend Circles (Medium) Input: [[1,1,0], [1,1,0], [0,0,1]] Output: 2 Explanation:Th ...
随机推荐
- <记录> Ubuntu16.04 安装Redis以及phpredis扩展
Linux下安装Redis 1.获取redis资源 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 2.解压 tar xzvf re ...
- 详述 IntelliJ IDEA 插件的安装及使用方法
首先,进入插件安装界面: Mac:IntelliJ IDEA -> Preferences -> Plugins; Windows:File -> Settings -> Pl ...
- ABAP-container拆分
1.界面 2.代码 *&---------------------------------------------------------------------* *& Report ...
- CSS3 之转动的风车
js 可以做动画,但是需要写很多代码:其实css也是可以做动画的,而且比js写的代码还少,理解也相对简单. 这里用到css3 的animation 属性,它配合着 @keyframes 规则来使用,可 ...
- 4.Python文件操作
文件内需要写入的内容 Seems the love I’ve ever known 看来,过去我所知道的爱情 Has always been the most destructive kind 似乎总 ...
- Linux下tomcat运行命令
tomcat启动 [root@master webapps]# /usr/local/tomcat7.0/bin/catalina.sh start startup.sh的源代码,其实就是执行 c ...
- Oracle数据库安装指南
文档使用声明 1.安装指导仅限测试环境(非生产环境)安装使用,生产环境oracle数据库建议按公司要求安装. 安装环境 1.SuSE10/SuSE11(64位) + Oracle11gR2 工具和安装 ...
- split()方法解析
split()方法用于将字符串分割为字符串数组. 废话不多说,直接贴代码: var str="How are you doing today?" console.log(str.s ...
- 扩展C#与元编程
扩展C#与元编程 https://www.cnblogs.com/knat/p/4580393.html https://www.cnblogs.com/knat/p/4584023.html 扩展C ...
- UIApplication 的学习
1.0 URL 的组成 == 协议头://主机名/路径 从iOS7 开始,系统提供了两种管理状态栏的方式,默认交给控制器去管理 2.0 旋转事件----> UIApplication --- ...