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. js 基础-&& || 逻辑与和逻辑或

    今天百度发现一个简化长if   else if 语句的方法,看起来及其强大,感觉这样虽然对系统性能提升没有帮助但是代码更简练了,分析了一番,下面先说说自己学到的理论. 首先要弄清楚js 中对于 变量, ...

  2. 软件工程导论课后习题Github作业(把一个英文句子中的单词次序逆序,单词中字母正常排列)

    Java源代码    package yly; import java.util.Scanner; public class ruanjian { public static void main(St ...

  3. SPRINT四则运算(第二天)

    1.小组成员: 李豌湄:master 江丹仪:产品负责人 2.现状: a.已经下载APP分析他们的界面.优缺点和闪光点  b.已改进代码添加功能 3.任务认领: 完成任务的第一个模块: a.下载五个类 ...

  4. JS对象复制(深拷贝、浅拷贝)

    如何在 JS 中复制对象 在本文中,我们将从浅拷贝(shallow copy)和深拷贝(deep copy)两个方面,介绍多种 JS 中复制对象的方法. 在开始之前,有一些基础知识值得一提:Javas ...

  5. Spring Cloud的Zuul的使用问题

    Zuul Client 放在移动App中,Zuul Server可以做集群. Zuul Client放在jar包吗?ios怎么办? Zuul与Spring Security配合使用,与Shiro做集成 ...

  6. Ehcache Monitor使用一例

    场景介绍:系统集成Shiro,使用Ehcache保存用户登录限制次数,常有用户密码被锁,影响工作效率. 在不考虑集成SSO,LDAP,也不引入身份校验,邮件,短信等解锁特性下.使用Ehcache Mo ...

  7. ionic2中如何使用自动生成器

    ionic generator是命令行的功能,ionic2自动帮我们创建应用程序,从而节省了大量的时间,并增加我们的速度来开发一个项目的关键部分. ionic generator使我们可以自动创建以下 ...

  8. 数据类型+内置方法 python学习第六天

    元组 用途:不可变的列表,能存多个值,但多个值只有取的需求而没有改的需求. 定义方式:在()内用逗号分隔开多个元素,可以存放任意类型的值. names=(‘alex’,’blex’,’clex’) 强 ...

  9. jmeter 参数化测试

    变量的值可以不改变,可以被定义一次,如果不使用,不会改变值. 在测试计划中,变量可以做为方便的频繁使用的表达式,或都作为在测试运行中的可能会改变的常量,例如 线程组中线程的数量.主机名称 在确定一个测 ...

  10. wamp下php报错session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file or directory

    报错提示 session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file ...