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 ...
随机推荐
- koa中间层 文件下载的请求转发
背景: 前端用a标签发起下载文档的get请求 node中间层接到get请求后将请求转发到java后端 java后端返回文档流传递给node中间层 好处: 后端的java业务逻辑层接口.数据库不向外部暴 ...
- 设计模式之笔记--外观模式(Facade)
外观模式(Facade) 定义 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 类图 描述 Facade:外观类,外观 ...
- Java容器---Arrays & Collections工具类
1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...
- 洛谷 P2077 红绿灯 题解
题目传送门 这道题一秒一秒的扫描一定会超时,所以就用一种O(N)的算法. #include<bits/stdc++.h> using namespace std; ],b[],c[],x= ...
- angular.js 验证码注册登录
css部分 header{ height: 50px; line-height: 50px; display: flex; } .callback{ text-align: left; display ...
- django的orm获取字段去重值
如果要用django的ORM获取一个表字段所有可能的去重值. 网上找了很多,都是用distinct关键字,但如何没有随后的order_by, 还是达不到要求的. 最后搞定. 参考URL http:// ...
- elk搭建实战
1 安装elasticsearch 1.1安装elasticsearch 相关中文文档:https://es.xiaoleilu.com 下载:从https://www.elastic.co/down ...
- 一个简单的AboutMe页面
Web2.0程序设计的小练习. Firefox下的效果 Chrome下的效果,套上了Helvetica,不知道为什么FF没有 (其实好像应该再优先加个Helvetica Neue的……呃……) 代码和 ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- 七牛刷新接口PHP实现
<?php require_once '../autoload.php'; use Qiniu\Auth; use Qiniu\Http\Client; $accessKey = 'access ...