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

#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. CentOS 7 BIND 搭建

    域名查找顺序 设置 /etc/host.conf 1. bind 安装 $ yum install bind bind-utilsnslookup (name server lookup) 在bind ...

  2. 深入浅出Koa

    深入浅出Koa(1):生成器和Thunk函数 Koa是个小而美的Node.js web框架,它由Express的原班人马打造的, 致力于以一种现代化开发的方式构建web应用. 通过这个系列,你将能够理 ...

  3. docker镜像与容器概念

    本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面的技术细节,能够很好的帮助读者深入理解Docker. 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(containe ...

  4. 【prim + kruscal 】 最小生成树模板

    来源:dlut oj 1105: Zhuo’s Dream Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 40 Solved: 14[Submit][St ...

  5. Mishka and Interesting sum

    Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input ...

  6. Android OpenGL ES(八)绘制点Point ..

    上一篇介绍了OpenGL ES能够绘制的几种基本几何图形:点,线,三角形.将分别介绍这几种基本几何图形的例子.为方便起见,暂时在同一平面上绘制这些几何图形,在后面介绍完OpenGL ES的坐标系统和坐 ...

  7. http请求连接

    1.在Info.plist中添加NSAppTransportSecurity类型Dictionary.2.在NSAppTransportSecurity下添加NSAllowsArbitraryLoad ...

  8. android笔记20170116

    封装http请求类,利用回调机制获取返回值 public interface HttpCallbackListener { void onFinish(String response); void o ...

  9. java 参数传值

    基本数据类型参数的传值,参数为基本数据类型 class Computer{ int add(int x,int y){ return x+y; } } public class Example4_6 ...

  10. 学习笔记——门面模式Facade

    门面模式,其实在我们不经意间已经使用了此设计模式.当我们需要将两个子系统,合并对外提供一个大的接口时,我们使用的就是门面模式.对外,子系统的接口是不可见的,只有我们的门面在.