PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
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 分) 并查集判断成环和联通+求树的深度的更多相关文章
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 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 ...
- [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 ...
- 1021 Deepest Root (25 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- phpstorm 断点调试总是从index.php的第一行开始
去掉勾选,重开phpstorm
- npm install 时 No matching version found for react-flow-design@1.1.14
执行 npm install时报错如下: 4017 silly pacote range manifest for react-highcharts@^16.0.2 fetched in 19ms40 ...
- html访问图片资源403问题(http referrer)
前言 之前碰到一个问题,就是html中通过img标签引入一个图片地址,报403.但是这个图片地址直接复制出来在地址栏打开,却是看得到的. 先说下解决方法: 在HTML代码的head中添加一句& ...
- Kotlin从零到精通Android开发
作者 博客地址 https://blog.csdn.net/aqi00 最新源代码 https://github.com/aqi00/kotlin 资源下载和内容勘误 https://blog.cs ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- ISO/IEC 9899:2011 条款6.5.6——加法操作符
6.5.6 加法操作符 语法 1.additive-expression: multiplication-expression additive-expression + multipli ...
- weui 可移动悬浮按钮
@CHARSET "UTF-8"; /** 右下角跳转按钮 跳转到列表 */ #list_note_icon { position: fixed; bottom: 10%; rig ...
- python初级 1 内存和变量
一.回顾: 1.什么是程序 一堆指令的集合 2.回想一下猜数游戏程序的特征: 1)需要输入(input) 2)会处理输入(process) 3)产生输出(output) 二.程序的一般特征:输入.处理 ...
- 构造方法(和python初始化变量类似)
public class Demo1 { int name2=1; public Demo1(int name) { name=name2; } public Demo1() { } public ...
- *438. Find All Anagrams in a String 找到字符串中所有字母异位词
1. 原始题目 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 201 ...