PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root
题意:
连接和非循环的图可以被认为是一棵树。树的高度取决于所选的根。现在你应该找到导致最高树的根。这样的根称为最深根。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,
第一行包含正整数N(<= 10000),它是节点的数量,因此节点从1到N编号。然后按N-1行,每个都通过给定两个相邻节点的数字来描述一个边。
输出规格:
对于每个测试用例,打印一行中最深的根。如果这样的根不是唯一的,
打印他们的数字增加的顺序。在给定的图形不是树的情况下,打印“错误:K组件”,其中K是图中连接的组件的数量。
思路:
我先用并查集看是不是树。然后任意点dfs,然后从得到的最远点为起始点再次dfs,这样直径两段的点就都可以得到了。然后在set里输出也可以。
- 测试点2:这里我卡了好久。是输出有几个连通分量。我循环的时候if(count > 1)直接break了。。害我一直错 = =。测试点2就是好多个连通分量。这里地方卡住可以说是十分rz了。
- 边的储存,因为题目限制65536kb,如果直接用数组int[10000][10000]至少用了800000000/1024 = 781 250kb所以不能直接用数组,可以用vector或者map动态储存
ac代码:
C++
// pat1021.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<unordered_map>
using namespace std;
unordered_map<int, vector<int>> map;
int pre[10005];
int n;
vector<int> hh;
bool visit[10005];
int mosth;
vector<int> res;
void findhighest(int height,int cur,vector<int>& h)
{
if (height > mosth)
{
mosth = height;
h.clear();
h.push_back(cur);
}
else if (height == mosth)
{
h.push_back(cur);
}
visit[cur] = true;
for (int i = 0; i < map[cur].size(); i++)
{
if (!visit[map[cur][i]])
{
findhighest(height + 1, map[cur][i], h);
visit[map[cur][i]] = false;
}
}
}
int findparent(int x)
{
return pre[x] == x ? pre[x] : findparent(pre[x]);
}
void merge(int x, int y)
{
int xx = findparent(x);
int yy = findparent(y);
if (xx == yy) return;
pre[yy] = xx;
}
int main()
{
memset(visit, false, sizeof(visit));
mosth = -1;
cin >> n;
for (int i = 1; i <= n; i++)
pre[i] = i;
int n1, n2;
for (int i = 1; i < n; i++)
{
cin >> n1 >> n2;
map[n1].push_back(n2);
map[n2].push_back(n1);
merge(n1, n2);
}
int count = 0;
for (int i = 1; i <= n; i++)
{
if (pre[i] == i) count++;
}
if (count > 1) cout << "Error: " << count << " components" << endl;
else
{
findhighest(0, 1, hh);
mosth = -1;
memset(visit, false, sizeof(visit));
findhighest(0, hh[0], res);
for (int i = 0; i < hh.size(); i++)
{
res.push_back(hh[i]);
}
sort(res.begin(), res.end());
for (int i = 0; i < res.size(); i++)
{
cout << res[i] << endl;
while (i + 1 < res.size() && res[i] == res[i + 1]) i++;
}
}
return 0;
}
PAT甲级1021. Deepest Root的更多相关文章
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- PAT甲级——A1021 Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 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 (25)(25 分)
1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- 安装ssh-keygen
转载自:http://www.daoan.com/forums/index.php?forumid=5&mods=topicdisplay&postid=4 sudo apt-get ...
- [转载]ACE的陷阱
转自: http://blog.csdn.net/fullsail/article/details/2915685 坦白说,使用这个标题无非是希望能够吸引你的眼球,这篇文章的目的仅仅是为了揭示一些AC ...
- Caffe学习系列(8):solver,train_val.prototxt,deploy.prototxt及其配置
solver是caffe的核心. net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_inter ...
- 想弄一弄tensorflow,先弄numpy
现在晚上凉快点了, 下班回家可以学会东东了.. 这次的书是一个印度人写的. 按着示例代码弄起先.. #!/usr/bin/env python # -*- coding: utf-8 -*- impo ...
- ceph存储池基本管理
一,设置默认存储池的pg或pgp的值(推荐100左右),在ceph.conf文件里增加: osd pool default pg num = osd pool default pgp num = 二, ...
- jquery放大镜非常漂亮噢
这个放大镜的代码挺简单滴效果也不错. <script> //QQ:496928838 微凉 $(function(){ $("#demo").enlarge( { // ...
- javascript 进制转换(2进制、8进制、10进制、16进制之间的转换)
//十进制转其他 var x=110; alert(x); alert(x.toString(8)); alert(x.toString(32)); alert(x.toString(16)); // ...
- phantomjs2.1 初体验
上次看了一下scrapy1.1的新手指南 决定写个小爬虫实验一下 目标网站是http://www.dm5.com/manhua-huofengliaoyuan准备爬取漫画火凤燎原的已有章节,将图片保存 ...
- YAHOO 35条前端优化建议(转)
Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.总结出了一系列 ...
- (11)go 数组和切片
一.数组 1.定义数组 定义时付给该类型默认值 2.初始化 箭头指向的数组代表数组的下标 3.数组遍历 方法1: 方法2: 二.切片 数组的数量不固定 1. 2. 3. string可以进行切片处理