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 题目大意:如果是连通图,则求连通图中点Vi到点Vj所有路径中最长(包含多对)的并打印出所有Vi与Vj。如果是非连通图,则打印出有几个子图。用并查集判断是否是连通图,随后用搜索来求最长路。
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define max 10002
int visit[max];
int a[max];
int N;
int distan[max];
vector<int>map[max];
int find(int x){
if(a[x]== x)return x;
find(a[x]);
}
void unio(int x,int y){
x = find(x);
y = find(y);
if(x != y){
a[x] = y;
}
}
int DFS(int key){
if(visit[key]==1)return 0;
visit[key]=1;
int i;
int sum = 0;
int m = map[key].size();
for(i=0;i<m;i++){
if(visit[map[key][i]]==0){
int tmp = DFS(map[key][i]);
if(sum < tmp){
sum = tmp;
}
}
}
return sum+1;
}
int main(){
scanf("%d",&N);
int i,j,t;
int s,d;
for(i=1;i<=N;i++){
a[i]=i;
}
for(i=1;i<N;i++){
scanf("%d%d",&s,&d);
unio(s,d);
map[s].push_back(d);
map[d].push_back(s);
}
int flag=0;
for(i=1;i<=N;i++){
if(a[i]==i){
flag++;
}
}
if(flag>1){
printf("Error: %d components",flag);
} else{
for(i=1;i<=N;i++){
memset(visit,0,sizeof(visit));
distan[i]=DFS(i);
}
int a=-1,b=0;
for(i=1;i<=N;i++){
if(distan[i]>a){
a=distan[i];
b=i;
}
}
for(i=1;i<=N;i++){
if(distan[i] == distan[b]){
printf("%d\n",i);
}
}
}
return 0;
}
1021. Deepest Root (25)的更多相关文章
- [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)(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 ...
- PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
随机推荐
- Linux内核分析 读书笔记 (第三章)
第三章 进程管理 3.1 进程 1.进程: 进程就是处于执行期的程序. 进程就是正在执行的程序代码的实时结果. 进程是处于执行期的程序以及相关的资源的总称. 进程包括代码段和其他资源. 2.线程:执行 ...
- 爬虫时http错误提示
在爬虫,请求网站的时候,有时候出现域名报错,所出现的代码所对应的意思:
- pandas修改全列的时间格式 无需使用apply
df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式
- spring 注入DI
web 项目的搭建 以注入对象的方式
- vue$ref
vue的$ref方法 可以在元素上template中直接添加ref属性 类似html的id属性 用来做选项卡的切换的
- Oracle 数据表误删恢复 Flashback
1. 前提条件. recyclebin 参数打开. 验证参数是否打开: SHOW PARAMETER RECYCLEBIN 2. 如果参数没有打开的话 需要打开,并且重启一下数据库方法为 alter ...
- linux_修改文件权限chmod 、改变文件/目录所有者chown
chmod使用事例 数字方法 chmod -R 751 [FILE] ugoa方法 chmod -R u=wrx,g=rx,o=x [FILE] 说明 chmod [OPTION] [MODE] [F ...
- 无法定位程序输入点 zend_empty_string php7.dll
phpstudy 在安装php_redis.dll php_redis.pdb 时,需要用到php_igbinary.dll扩展.但我下载的版本不对.下载的是7.2版本的. 所以报以上错误.可选择版本 ...
- SAP顾问岗位要求
岗位职责: 1.负责SAP系统各模块日常运维工作,解决用户在系统操作过程中遇到的问题: 2.评估用户需求(新需求.功能优化)的可实现性,完成SAP系统及相关系统的配置调整及功能实现: 3.负责CRM等 ...
- 归并排序详解(python实现)
因为上个星期leetcode的一道题(Median of Two Sorted Arrays)所以想仔细了解一下归并排序的实现. 还是先阐述一下排序思路: 首先归并排序使用了二分法,归根到底的思想还是 ...