problem

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

tip

answer

#include <cassert>
#include <set>
#include <iostream>
#include <vector>
#include <cstring> #define Max 10010
#define fi first
#define se second
using namespace std; int N;
int Root[Max], V[Max];
vector<int > E[Max];
set<int > S, Last;
set<int>::iterator it; void Init(){
for(int i = 1; i <= N; i++){
Root[i] = i;
}
} int Find(int i){
if(Root[i] != i) Root[i] = Find(Root[i]);
return Root[i];
} void Union(int a, int b){
int ra = Find(a);
int rb = Find(b);
if(ra == rb) return ;
Root[ra] = Find(Root[rb]);
} int TreeNum() {
int num = 0;
for(int i = 1; i <= N; i++){
if(Root[i] == i) num++;
}
return num;
} int DFS(int t, int deep, int &maxD){
if(deep > maxD){
maxD = deep;
S.clear();
S.insert(t);
}
if(deep == maxD){
S.insert(t);
}
V[t] = 1;
for(int i = 0; i < E[t].size(); i ++){
if(!V[E[t][i]]){
DFS(E[t][i], deep+1, maxD);
}
}
} void InitV(){
memset(V, 0, sizeof(V));
} void PrintStatus(){
for(it = S.begin(); it != S.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
}
int main(){
// freopen("test.txt", "r", stdin) ;
ios::sync_with_stdio(false);
//N<= 10000
cin>>N;
if(N == 0){
assert(false);
cout<<"0"<<endl;
}
Init();
int a, b;
for(int i = 0; i < N-1; i++) {
cin>>a>>b;
E[a].push_back(b);
E[b].push_back(a);
Union(a, b);
}
if(TreeNum() > 1) {
cout<<"Error: "<<TreeNum()<<" components";
}else{
int maxD = 0, deepN;
DFS(1, 0, maxD);
// PrintStatus();
for(it = S.begin(); it != S.end(); it++){
Last.insert(*it);
deepN = *it;
}
maxD = 0;
// cout<<deepN<<endl;
InitV();
DFS(deepN, 0, maxD);
// PrintStatus();
for(it = S.begin(); it != S.end(); it++){
Last.insert(*it);
}
for(it = Last.begin(); it != Last.end(); it++){
cout<<*it<<endl;
}
}
return 0;
}

exprience

  • acyclic 无环的

1021 Deepest Root (25)(25 point(s))的更多相关文章

  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. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  4. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  5. MySQL5.7.25(解压版)Windows下详细的安装过程

    大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...

  6. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  7. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  8. PAT 甲级 1006 Sign In and Sign Out (25)(25 分)

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...

  9. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  10. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

随机推荐

  1. 学号20155311 2016-2017-2 《Java程序设计》第7周学习总结

    学号20155311 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治时间,简称GMT时间,由观察太阳而得来:世界时,UT:国际原子时,T ...

  2. Oracle中varchar2(XX)和varchar2(XX byte)区别

    这两个相不相同是由参数NLS_LENGTH_SEMANTICS决定的,有两个单位,char(字符)或者字节(byte),该参数默认值为BYTE. alter session set nls_lengt ...

  3. PHP 生成、识别二维码及安装相关扩展/工具

    2018-02-20 00:30:26  更新:推荐新扩展(极力推荐) 这篇文章里用的两个二维码扩展都有些问题和麻烦:phpqrcode(生成二维码)的源码有点小 bug: 而 php-zbarcod ...

  4. Java不为人知的小秘密

    Java中的main方法必须有一个外壳类,而且必须是静态的! Java中的所有函数都属于某个类的方法,所以main方法也不例外,必须放在一个类中才能编译运行. 例如: public class tex ...

  5. SQLAlchemy-介绍安装

    一:概述 SQLAlchemy的SQL工具包和对象关系映射是一个全面的工具集,用来处理数据库和Python. 它有几个不同的功能领域,可以单独使用或组合使用. 所示的主要组件,组件依赖关系组织成层: ...

  6. 缓存数据库-redis安装和配置

    一:redis安装 python操作redis分为两部分,一为安装redis程序 二是安装支持python操作redis的模块 1)安装redis redis 官方网站:http://www.redi ...

  7. apache代理配置https

    原文:https://mp.weixin.qq.com/s/Tw4UzX73Q7MSw3GJXnpN8A 微信小程序开发https设置 2017-04-06 格里菲斯 互联网工作者 微信官方规定小程序 ...

  8. Spring框架的基本使用(IOC部分)

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. Spring的好处 1.方便解耦,简化开发: Spring就是一个大工厂,专门负责生成Bean,可以将所有对象创建和依赖关 ...

  9. Centos中查询目录中内容命名ls

    首先解释下这块, root代表当前登录用户,localhost代表主机名, ~代表当前主机目录,  #代表用户权限   #表示超级用户,$表示普通用户: 查询目录中内容命令 ls  (list缩写) ...

  10. Python学习笔记:一手漂亮的Python函数

    使用类和函数定义模型 函数是抽象和封装的基本方法之一 重构函数  -- 命名合理  -- 具有单一功能  -- 包含文档注释  -- 返回一个值  -- 代码不超过 50 行  -- 幂等函数,尽可能 ...