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 (≤104) 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

交了一发暴力,居然过了,关键就是这个求树的深度,这题的主要坑点在于,如果不是一颗树要输出那个信息,如果成环了但是只有一个连通块,要输出

Error: 1 components

所以注意判断逻辑

int dfs(int x)
{
//de(x);
vis[x]=1;
int ma=0;
for(int i=0;i<(int)G[x].size();i++){
int v=G[x][i];
if(vis[v])continue;
ma=max(ma,dfs(G[x][i]));
}
return ma+1;
}
#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std; const int maxn=1e4+5;
int father[maxn]; // 储存i的father父节点 void makeSet(int n) {
for (int i = 1; i <=n; i++)
father[i] = i;
} int findRoot(int x) { // 迭代找根节点
int root = x; // 根节点
while (root != father[root]) { // 寻找根节点
root = father[root];
}
while (x != root) {
int tmp = father[x];
father[x] = root; // 根节点赋值
x = tmp;
}
return root;
} void Union(int x, int y) { // 将x所在的集合和y所在的集合整合起来形成一个集合。
int a, b;
a = findRoot(x);
b = findRoot(y);
father[a] = b; // y连在x的根节点上 或father[b] = a为x连在y的根节点上;
}
vector<int>G[maxn];
/*
5
1 2
1 3
1 4
2 5
*/
int maxx;
int depth[maxn];
int vis[maxn];
int dfs(int x)
{
//de(x);
vis[x]=1;
int ma=0;
for(int i=0;i<(int)G[x].size();i++){
int v=G[x][i];
if(vis[v])continue;
ma=max(ma,dfs(G[x][i]));
}
return ma+1;
}
int main()
{
int n;
cin>>n;
makeSet(n);
int m=n-1;
int a,b;
int tree_flag=true;
while(m--)
{
scanf("%d%d",&a,&b);
if(findRoot(a)!=findRoot(b)){
Union(a,b);
G[a].push_back(b);
G[b].push_back(a);
}
else tree_flag=false; }
//int components=0;
set<int>s;
for(int i=1;i<=n;i++)
{
//de(i);
//de(findRoot(i));
s.insert(findRoot(i));
}
//de(s.size());
if(tree_flag==false||s.size()!=1)
{
printf("Error: %d components\n",(int)s.size());
return 0;
}
maxx=0;
each(i,1,n)
{
memset(vis,0,sizeof(vis));
//de(i);
depth[i]=dfs(i);
}
each(i,1,n)
{
maxx=max(maxx,depth[i]);
}
each(i,1,n)
{
if(depth[i]==maxx)
{
cout<<i<<endl;
}
} return 0;
}

PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度的更多相关文章

  1. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  2. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  3. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  4. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  5. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  6. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  7. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  8. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

  9. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. ISO/IEC 9899:2011 条款6.2——概念

    6.2 概念 6.2.1 标识符的作用域 6.2.2 标识符的连接 6.2.3 标识符的名字空间 6.2.4 对象的存储持久性 6.2.5 类型 6.2.6 类型的表示 6.2.7 兼容类型与组合类型 ...

  2. 查看Oracle中是否有锁表

    转: 查看Oracle中是否有锁表 2018-04-23 17:59 alapha 阅读(19450) 评论(0) 编辑 收藏 一.用dba用户登录,或者将用户赋权为DBA用户 命令: su - or ...

  3. 品优购商城项目(二)AngularJS、自动代码生成器、select2下拉多选框

    品优购商城想项目第二阶段 AngularJS.自动代码生成器.select2下拉多选框 完成了课程第三天.第四天的的任务. 1.学习了AngularJs前端的mvc分层思想,js部分分成control ...

  4. centos 安装最新版git

    对个人而言,gitlab有点浪费资源,占内存太大,一个博客服务器,配置比较低,用gitlab太浪费了.(公司使用gitlab,这个适合公司团队使用) 前提条件,放行git端口,防火墙添加放行规则,将3 ...

  5. python web开发——django学习(一)第一个连接mysql数据库django网站运行成功

    1.新建一个项目 2.新建一些文件夹方便管理 3.新建一个项目叫message  4.连接数据库 python web开发Django连接mysql 5.在数据库里自动生成django的表  6.运行 ...

  6. spring 配置事务管理器

    在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.tr ...

  7. Jackson解析自定义json到实体类

    json文本 { "status": 0, "result": { "final": true, "hypotheses" ...

  8. array_fill 填充数组内容

    <?php $a = array_fill(, , 'banana'); $b = array_fill(-, , 'pear'); print_r($a); print_r($b) Array ...

  9. web端自动化——selenium项目集成HTML测试报告

    参考内容: 虫师:<selenium2自动化测试实战——基于python语言> PS:书中的代码,只能做参考,最好还是自己码一码,不一定照搬就全是对的,实践出真知... 随着软件不断迭代功 ...

  10. 编译Cython代码时遇到的问题: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

    使用python setup.py build_ext --inplace命令编译cython代码, 出现以下错误: Compiling cython_example.pyx because it c ...