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

#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. 其它网页可以上网,IE浏览器打不开网页的解决办法

    下面是自己引用别人的,作为自己的备注 昨天由于安装了多款软件,今天开机发现IE浏览器打不开了,废了些周折终于,修复了IE浏览器,现将ie浏览器打不开网页的经验分享给大家,希望此经验对于出现过此类情况的 ...

  2. 缩放系列(三):一个可以手势缩放、拖拽、旋转的layout

    弄了一个下午,终于搞出来了,PowerfulLayout 下面是一个功能强大的改造的例子: 可以实现以下需求: 1.两个手指进行缩放布局 2.所有子控件也随着缩放, 3.子控件该有的功能不能丢失(像b ...

  3. url重写步骤

    url重写:在global文件中,在application_BeginRequest事件中1:获取URL string url=Request.AppRelativeCurrentExecutionF ...

  4. launchMode传递参数注意startActivityForResult

    Activity1 到Activity2 用startActivityForResult 如果Activity2的launchMode为 singleInstance 和 singleTask 都会启 ...

  5. sql server 数据库导出表里所有数据成insert 语句

    有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...

  6. c++中string的用法

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  7. java实现类似qq的窗口对聊

    Swing实现界面,socket实现通信 package com.learn.talk; import java.awt.BorderLayout; import java.awt.Dimension ...

  8. echarts学习总结(二):一个页面存在多个echarts图形,图形自适应窗口大小

    如上图所示一个页面有两个echarts图形:散点图和折线图,如果还写为: myChart.setOption(option); window.onresize = myChart.resize; 则只 ...

  9. JPA 系列教程7-双向多对多

    双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...

  10. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...