先并查集判断连通性,然后暴力每个点作为根节点判即可。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; struct Edge
{
int a,b;
}e[];
int n,sz,f[],dep,U;
bool flag[];
vector<int>g[]; int Find(int x)
{
if(x!=f[x]) return f[x]=Find(f[x]);
return f[x];
} void dfs(int x,int d)
{
dep=max(dep,d); flag[x]=;
for(int i=;i<g[x].size();i++)
if(flag[g[x][i]]==) dfs(g[x][i],d+);
} void DFS(int x,int d)
{
U=max(U,d); flag[x]=;
for(int i=;i<g[x].size();i++)
if(flag[g[x][i]]==) DFS(g[x][i],d+);
} int main()
{
scanf("%d",&n); sz=n;
for(int i=;i<=n;i++) {g[i].clear();f[i]=i;}
for(int i=;i<=n-;i++)
{
scanf("%d%d",&e[i].a,&e[i].b);
g[e[i].a].push_back(e[i].b);
g[e[i].b].push_back(e[i].a);
}
for(int i=;i<=n-;i++)
{
int fx=Find(e[i].a), fy=Find(e[i].b);
if(fx!=fy) { sz--; f[fx]=fy; }
}
if(sz!=) printf("Error: %d components\n",sz);
else
{
dep=;
for(int i=;i<=n;i++)
{
memset(flag,,sizeof flag);
dfs(i,);
} for(int i=;i<=n;i++)
{
U=;
memset(flag,,sizeof flag);
DFS(i,);
if(U==dep) printf("%d\n",i);
}
}
return ;
}

PAT (Advanced Level) 1021. Deepest Root (25)的更多相关文章

  1. PTA (Advanced Level) 1021 Deepest Root

    Deepest Root A graph which is connected and acyclic can be considered a tree. The hight of the tree ...

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

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

  3. [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 ...

  4. 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 ...

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

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

  6. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  7. 1021. Deepest Root (25)

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

  8. 1021 Deepest Root (25)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  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. zend笔记

    ZEND_STRL(str)  等价于 (str), (sizeof(str)-1) ZEND_STRS(str)等价于 (str), (sizeof(str))

  2. win10快捷键大全

    win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...

  3. 【RMQ】 区间最值查询详解

    1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...

  4. RS485 介绍

    一.RS485总线介绍: RS485总线是一种常见的串行总线标准,采用平衡发送与差分接收的方式,因此具有抑制共模干扰的能力.在一些要求通信距离为几十米到上千米的时候,RS485总线是一种应用最为广泛的 ...

  5. BigDecimal加减乘除运算

    java.math.BigDecimal.BigDecimal一共有4个够造方法,让我先来看看其中的两种用法: 第一种:BigDecimal(double val)Translates a doubl ...

  6. java 反射的实例

    JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制 ...

  7. ubuntu12.04的NFS配置

    安装nfs: #sudo apt-get install nfs-kernel-server ubuntu12.04中的已经是最新版本了,无需安装 打开/etc/exports文件,在末尾加入: /h ...

  8. mysql 入门 基本命令

    MYSQL入门学习之一:基本操作  1.登录数据库    www.2cto.com     命令:mysql -u username –p (mysql -h主机地址 -u用户名 -p用户密码)   ...

  9. monkeyrunner对比屏幕局部图像.getSubImage()

    monkeyrunner对比屏幕局部图像.getSubImage() monkeyrunner执行测试时使用.takeSnapshot()截图,默认截取整个屏幕,包含了系统的状态栏.真实手机状态栏中包 ...

  10. 指针--摘自C++技术网 作者dx

    “指针是什么?”“指针就是一种数据类型.”“你确定?”“那数据类型是什么?额,这个???类型就是类型,还能怎么解释嘛.”“指针有多少种?”“指针有好多种,比如整型指针,字符指针等等.”“指针是怎么确定 ...