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 ( ...
随机推荐
- ubuntu内窗口最大最小化
我用的dell笔记本,开始的时候,需要修改dell笔记本的BIOS,里面有一个 Funtion Key 一开始定义的行为是多媒体的,需要改成默认行为,具体记不清了, 反正知道这个,肯定知道哪里找. 然 ...
- atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模
atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模 1. 组件结构 1 2. startDraw 开始抽奖流程建模 1 3. 抽奖算法 2 作者:: 老哇的爪子 Attilax 艾龙, ...
- shell判断文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- oracle autotrace
--======================= -- 启用 AUTOTRACE功能 --======================= AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL ...
- 跟着百度学PHP[12]-文件处理 文件 目录
00x1 文件的属性 文件属性 <?php //-------------------------定义大小转换函数--------------- function changesize_dw($ ...
- C语言 · 复数归一化
算法提高 复数归一化 时间限制:1.0s 内存限制:512.0MB 编写函数Normalize,将复数归一化,即若复数为a+bi,归一化结果为a/sqrt(a*a+b*b) + i ...
- Java反射机制在代理模式中的使用
代理模式的核心思路就是一个接口有两个子类,一个子类完成核心的业务操作,另一个子类完成与核心业务有关的辅助性操作. 代理模式分为静态代理模式和动态代理模式. 静态代理模式: //接口类 interfa ...
- 手把手教你利用微软的Bot Framework,LUIS,QnA Maker做一个简单的对话机器人
最近由于要参加微软亚洲研究院的夏令营,需要利用微软的服务搭建一个对话Bot,以便对俱乐部的情况进行介绍,所以现学了几天,搭建了一个简单的对话Bot,期间参考了大量的资料,尤其是下面的这篇博客: htt ...
- 如何使用PHP开发高效的WEB系统
PHP是一个非常优秀的工具,它能够简单,也能够复杂.不一样的项目,应该用不一样的PHP. 小项目 - 简单而直接的PHP 一般对于一个功能页面在20下面的站点.我们能够用一个非常easy的框架结构来 ...
- go web框架推荐
https://blog.usejournal.com/top-6-web-frameworks-for-go-as-of-2017-23270e059c4b https://www.zhihu.co ...