PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可。
#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)的更多相关文章
- 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 ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- [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 ...
- 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 (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 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 ...
- 1021 Deepest Root (25 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- 把aspx页面输出成xml的方法注意事项
先贴代码 Response.Charset = "gb2312"; Response.ContentType = "text/xml"; Response.Co ...
- 去掉uitableveiw多余的分割线
UIView *v = [[UIView alloc] initWithFrame:CGRectZero]; [_tableView setTableFooterView:v];
- jQuery实现DIV拖动
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- centos7如何关闭防火墙
1.centos7自带了firewall,而不是iptables: 关闭firewall: service firewalld stop 或者: systemctl stop firewalld 禁止 ...
- IT项目各阶段管理
- CodeIgniter 如何去掉 Index.php
步骤; 1.打开你的WEB服务器的httpd.conf文件. 2.找到LoadModule rewrite_module modules/mod_rewrite.so这行,把该行前的#去掉.保存该文件 ...
- Android图片处理神器BitmapFun源码分析
作为一名Android开发人员,相信大家对图片OOM的问题已经耳熟能详了,关于图片缓存和解决OOM的开源项目也是相当的多,被大家熟知的就是Universal_image_loader和Volley了, ...
- word2010无法打开文件时的一点对策
word2010无法打开文件时的一点对策 1. Word 安全模式启动,点击「开始」,在搜索栏中输入winword /safe并回车,测试问题是否依然存在? 2. 正常启动Word,点击“文件”—“选 ...
- github入门操作
一.更新github上的已有项目: 将repository clone到本地 shanyu@debian:~/Git$ git clone https://github.com/xunbu7/Hell ...
- Linux环境下Android JNI程序的编译
尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8993493 在android开发中,有时候需要编写一些C/C++代码,这时候 ...