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 ( ...
随机推荐
- keepalived virtual_router_id 44
在同一局或网内如果有多个keepalived 的话 virtuall_router_id 44 (不能相同,但同一对,是一定相同)
- WWDC 2014 Session笔记 - iOS界面开发的大一统
本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 What's New in Cocoa Touch Building Adaptive Apps with UIKit Wh ...
- UITableView__cell 距tableview顶端有间距
UITableView__cell 距tableview顶端有间距 如何去掉这个间距呢?解决方法如下: //top 为cell距顶端的间距 (一般为负值) self.formTable.con ...
- QQ会员活动运营平台架构设计实践——高效自动化运营
QQ会员活动运营平台(AMS),是QQ会员增值运营业务的重要载体之一,承担海量活动运营的Web系统.在过去四年的时间里,AMS日请求量从200-500万的阶段,一直增长到日请求3-5亿,最高CGI日请 ...
- ngx_lua 随笔
--[[ test --]] ngx.header.content_type = "text/plain"; --输出头部 local user = ngx.var.arg_use ...
- 我的vim 自动实例括号函数
不废话,直接上代码: """"""""""""""" ...
- lmbench
lmbench作为性能检测工具的一种,提供内存,网络,内核等多方面的测试工具.是benchmark众多功能测试软件中的一种.几天了解了下,记录于此. 参考链接 http://www.bitmover. ...
- java 多线程 3 synchronized 同步
多任务编程的难点在于多任务共享资源.对于同一个进程空间中的多个线程来说,它们都共享堆中的对象.某个线程对对象的操作,将影响到其它的线程. 在多线程编程中,要尽力避免竞争条件(racing condit ...
- TrustZone——开源库—Linaro—OP-TEE
想研究安全系统源代码的有福气了.曾经OVOS的代码缺少TA相关的实现. 这次的版本号,基本框架都有了.先看看架构图吧. 几家大公司做的,可能是ST牵头.页面有ST的LOGO. 代码质量较高. 未来也会 ...
- json转datatable(正则表达式的方法)
/// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...