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 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))的更多相关文章
- [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 ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- MySQL5.7.25(解压版)Windows下详细的安装过程
大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- 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 ...
- 【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 ...
- 【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 ...
随机推荐
- 阿里云mysql数据库设置让公网访问客户端访问
第一步 首先使用root登入你的mysql ./mysql -u root -p 你的密码 第二步 备注:也可以添加一个用户名为yuancheng,密码为123456,权限为%(表示任意ip都能连接) ...
- Python 控制流、列表生成式
Python的三种控制流.认识分支结构if.认识循环结构while.认识循环结构for.Break语句.Continue语句.
- 【LinuxC】GCC编译C程序,关闭随机基址
1.编译.链接和运行程序 C代码示例: #include <stdio.h> #include <stdlib.h> int main() { printf("hel ...
- Python标准库笔记(10) — itertools模块
itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...
- 一个不错的linux学习资料下载的网址
本文比较完整的讲述GNU make工具,涵盖GNU make的用法.语法.同时重点讨论如何为一个工程编写Makefile.作为一个Linux程序员,make工具的使用以及编写Makefile是必需的. ...
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
思路来源:http://bbs.csdn.NET/topics/390819824,引用该页面某网友提供的方法. 题目:我现在有100个任务,需要多线程去完成,但是要限定同时并发数量不能超过5个. 原 ...
- APUE-文件和目录(八)文件时间
文件的时间 与文件相关的三个时间值: 访问时间:最后一次访问文件的时间.例如,cat命令会修改这个时间. 修改时间:文件内容最后一次被修改的时间. 状态更改时间:文件的i节点最后一次被修改的时间.例如 ...
- Scala中的"null" 和“_”来初始化对象
Alternatives Use null as a last resort. As already mentioned, Option replaces most usages of null. I ...
- 最后一面《HR面》------十大经典提问
1.HR:你希望通过这份工作获得什么? 1).自杀式回答:我希望自己为之工作的企业能够重视质量,而且会给做得好的员工予以奖励.我希望通过这份工作锻炼自己,提升自己的能力,能让公司更加重视我. a.“我 ...
- 如何用纯CSS布局两列,一列固定宽度,另一列自适应?
大家都知道好多网站都是左右布局的,很多公司在笔试和面试环节也常常问这个问题.一个去网易的师兄说14年腾讯面试的时候问过这个问题,网易在笔试和面试时候也问过这个问题,还有很多互联网公司也都涉及到这个问题 ...