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的更多相关文章

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

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

  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

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

  4. PAT甲级——A1021 Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. PAT 1021 Deepest Root[并查集、dfs][难]

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

  6. [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 ...

  7. 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 ...

  8. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  9. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. angular导出文件保存在本地

    $scope.ev_click = function(obj){ var ev = document.createEvent("MouseEvents"); ev.initMous ...

  2. 利用__attribute__((section()))构建初始化函数表【转】

    转自: https://mp.weixin.qq.com/s?__biz=MzAwMDUwNDgxOA==&mid=2652663356&idx=1&sn=7797629530 ...

  3. 在Ubuntu上使用pip安装错误 read timed out 处理方法

    在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.

  4. 简约而不简单的Django

    本文面向:有python基础,刚接触web框架的初学者. 环境:windows7   python3.5.1  pycharm专业版  Django 1.10版 pip3 一.Django简介 百度百 ...

  5. sed实践

    后来也找到一篇文章讲的很详细: http://www.cnblogs.com/ctaixw/p/5860221.html --------------------------------------- ...

  6. linux shell awk实现实时监控网卡流量脚本

    goodtools! 原文 awk 'BEGIN{ OFMT="%.3f"; devf="/proc/net/dev"; while(("cat &q ...

  7. 常用开放api【长期更新】

    获取时间: 苏宁:http://quan.suning.com/getSysTime.do 淘宝:http://api.m.taobao.com/rest/api3.do?api=mtop.commo ...

  8. 第三方登陆微博、qq、微信

    源文:http://blog.csdn.net/tivonalh/article/details/60954373 假设是已经申请完成各平台开发者账号. 先来简单的,微博和QQ 微博: 引入微博JS ...

  9. elementUI 学习入门之 layout 布局

    layout 布局 通过基础的 24 分栏,可进行快速布局 基础布局 使用单一分栏创建基础的栅格布局, 通过 span 属性指定每栏的大小 <el-col :span="8" ...

  10. 转:Meltdown Proof-of-Concept

    转:https://github.com/IAIK/meltdown Meltdown Proof-of-Concept What is the difference between Meltdown ...