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. 软件项目第一次Sprint总结

    成果评分表: 组名 分数 原因 9-652 6 界面和谐生动,可运行,在目前阶段可时间基本操作 hzsy -2 代码下载,但实现安卓和相机调用 JYJe族 -1 实现安卓界面,完成一项功能,做得少 结 ...

  2. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  3. iOS GCD中级篇 - dispatch_semaphore(信号量)的理解及使用

    理解这个概念之前,先抛出一个问题 问题描述: 假设现在系统有两个空闲资源可以被利用,但同一时间却有三个线程要进行访问,这种情况下,该如何处理呢? 或者 我们要下载很多图片,并发异步进行,每个下载都会开 ...

  4. Golang的panic和recover

    panic 关键字panic的作用是制造一次宕机,宕机就代表程序运行终止,但是已经“生效”的延迟函数仍会执行(即已经压入栈的defer延迟函数,panic之前的). 为什么要制造宕机呢?是因为宕机不容 ...

  5. MySQL基础~~表结构操作

    登录数据库服务器 mysql -h127.0.0.1 -uroot -p123456 创建数据库 create database test; 显示所有数据库 show databases; 指定要操作 ...

  6. 转帖: Serverless架构模式简介

    Serverless架构模式简介   原贴地址:https://blog.csdn.net/chdhust/article/details/71250099?utm_medium=referral&a ...

  7. 转帖 Oracle 主键的处理方法 http://www.cnblogs.com/Richardzhu/p/3470929.html

    Oracle之主键的创建.添加.删除操作   一.创建表的同时创建主键约束 1.1.无命名 SQL> create table jack (id int primary key not null ...

  8. centos7黑客帝国装逼

    黑客帝国既视感 搜 cmatrix 然后放到本地解压缩 ,安装 yum install ncurses-devel./configure && make && make ...

  9. c-lodop云打印实现手机打印 JS语句打印

    Lodop和c-lodop目前只能安装到windows操作系统上,但是其他操作系统可通过向C-Lodop安装的电脑发送打印任务,实现手机广域网或局域网打印,打印语句也是简单的JS语句,可以轻松实现云打 ...

  10. 文件IO流完成文件的复制(复杂版本主要用来演示各种流的用途,不是最佳复制方案哦)

    package io; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import j ...