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)的更多相关文章

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

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

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

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

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

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

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

  6. 1021 Deepest Root (25 分)

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

  7. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

  8. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

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

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

随机推荐

  1. Java实验二

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计 班级:1351    姓名:黄君如  学号:20135117 成绩:             指导教师:娄 ...

  2. Python学习笔记 -- 第五章

    模块 使用模块可以提高了代码的可维护性.其次,编写代码不必从零开始.当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Python内置的模块和来自第三方的模块: ...

  3. ppm\℃是什么意思/

    转自http://www.zybang.com/question/b158a106b4e39d8fdb2b93fd3777a00f.html 在基准电压的数据手册里,我们会找到一个描述基准性能的直流参 ...

  4. CentOS(6.8)7 安装 Mysql 5.7

    https://blog.csdn.net/zyw_java/article/details/70949596 https://blog.csdn.net/yzl11/article/details/ ...

  5. HTML 5 placeHolder

    <html> <body> <input type="text" id="idNum" placeholder="pla ...

  6. PSP(5.4——5.10)以及周记录

    1.PSP 5.4 14:00 17:00 70 110 讨论班 A Y min 5.5 10:00 16:50 125 285 Cordova A Y min 5.6 13:30 15:00 35 ...

  7. aliyun centos14.04 trusty 上安装docker1.12.1

    现在apt这边拿到的docker最新版本就是1.12.1 其实本来这次不准备记录了,本以为一帆风顺的安装最后还是遇到了一点坑,aliyun的锅,卡成狗无法下载.青岛机房 1.更新源,然后安装ca-ce ...

  8. 【Java】 枚举类

    如果要定义一个枚举类: public enum Size { SAMLL, MEDIUM, LARGE, EXTRA, EXTRA_LARGE}; 实际上,这个声明定义的类型是一个类,它刚好有4个实例 ...

  9. map()实现zip()功能

    c = (map(lambda x,y:(x,y),[1,2,3],["abd","def","ghi"]))print(list(c)) ...

  10. 遍历List、Map删除元素

    遍历List删除元素 方法一: List<String> list = new ArrayList<>(); list.add("1"); list.add ...