PAT 甲级 1021 Deepest Root (并查集,树的遍历)
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 (<=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,
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector> using namespace std;
const int maxn=1e4;
int n;
struct Node
{
int value;
int next;
}edge[maxn*2+5];
int father[maxn+5];
int head[maxn+5];
int vis[maxn+5];
int num[maxn+5];
int tag[maxn+5];
int tot,cnt;
void add(int x,int y)
{
edge[tot].value=y;
edge[tot].next=head[x];
head[x]=tot++;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void dfs(int root,int deep)
{
vis[root]=1;
int tag=0;
for(int i=head[root];i!=-1;i=edge[i].next)
{
int y=edge[i].value;
if(!vis[y])
{
tag=1;
dfs(y,deep+1);
}
}
if(!tag)
num[cnt]=max(num[cnt],deep);
}
int main()
{
scanf("%d",&n);
int x,y;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
father[i]=i;
tot=0;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
int fx=find(x);
int fy=find(y);
if(fx!=fy)
father[fx]=fy;
add(x,y);
add(y,x);
}
memset(tag,0,sizeof(tag));
int res=0;
for(int i=1;i<=n;i++)
{
find(i);
tag[father[i]]=1;
}
for(int i=1;i<=n;i++)
if(tag[i])
res++;
if(res>1)
printf("Error: %d components\n",res);
else
{
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
cnt=i;
dfs(i,0);
}
int ans=0;
for(int i=1;i<=cnt;i++)
ans=max(ans,num[i]);
for(int i=1;i<=cnt;i++)
if(num[i]==ans)
printf("%d\n",i);
}
return 0;
}
PAT 甲级 1021 Deepest Root (并查集,树的遍历)的更多相关文章
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- 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 甲级 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 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- PAT甲级——A1021 Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
随机推荐
- 为什么会找不到D层文件?
近期两天在重装系统,今天好不easy把各种东西都装齐全了,再打开我的机房收费系统,就提演示样例如以下错误: 看到这个问题.我感觉非常熟,由于曾经也遇到过两次这个问题,都是改了下D层的编译路径.改到了U ...
- 第二节 JVM优化应用以及知识总结
在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...
- iPhone应用程序的启动过程
Phone的入口函数main,这之后它有是怎样启动应用程序,初始化的呢,这些都是通过 UIApplicationMain 来实现的. 其启动的流程图大致如下图所示: 1 int retVal = UI ...
- github上搭建网站前台页面
其实就是把html页面提交到github,为了能在线演示: 1. 首先在github网站找到你的项目 2. 点击设置 3. 找到这几个选项,选择master branch打钩,然后保存 4. 然后就会 ...
- WP架构设计(一)MVVM回顾
[MVVM的定义] MVVM的目的是什么? 简单总结起来一句话:分离UI逻辑和业务逻辑.这一点和被大家熟知的MVP和MVC是一致的. 下面详细来说明下这个问题,下面一段英文来自Msdn ...
- 一题关于PHP的CTF
if(isset($_GET['time'])){ if(!is_numeric($_GET['time'])){ echo 'The time must be number.'; }else if( ...
- Farey Sequence(欧拉函数)
题意:给出式子F F中分子分母互质,且分子小于分母 例: F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/ ...
- openresty 视频
http://v.163.com/paike/V8H1BIE6U/V949ER8RD.html#from=search
- C++ new delete操作符
//new delete操作符 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; /* 1.n ...
- 人脸验证算法Joint Bayesian详解及实现(Matlab)
python http://blog.csdn.net/cyh_24/article/details/49059475 github https://github.com/johnnyconstant ...