[PAT] A1021 Deepest Root
【题目大意】
给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大。输出所有满足要求的可以作为树根的结点。
【思路】
方法一:模拟。
1 连通、边数为n-1的图一定是一棵树。因此先判断连通图个数(用DFS遍历图,从而计算连通图个数),等于1则能形成一棵树。
2 遍历每一个节点,假设它为根节点构造一棵树。
方法二:
1 由于连通、边数为n-1的图一定是一棵树,因此需要判断给定数据是否能使图连通。
2 确定图连通后,则确定了树,选择合适根结点使树高最大的做法为:
先任意选择一个结点,从该节点开始遍历整棵树,获取能达到的最深的结点,记为集合A;然后从集合A中任意一个结点出发遍历整棵树,获取能达到的最深顶点,记为结点集合B。集合A与B的并集就是所求结果。
https://blog.csdn.net/hy971216/article/details/82252707
https://blog.csdn.net/qq_38677814/article/details/80859998
https://blog.csdn.net/sinat_29278271/article/details/47934611
【tips】
1 非二叉树其实可以想象成一个特殊的图来解决。像这一题,用的其实全都是图的知识来解决,因为其考点不在父子关系上。
2 也可以使用并查集判断连通图个数(未实践过):每读入一条边的两个端点,判断这两个端点是否属于相同的集合,如果不同,则将它们合并到一个集合中,当处理完所有边后根据最终产生的集合个数是否为1来判断给定的图是否连通。
【AC代码】
方法一:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define N 10002
vector<int>G[N];
int n;
int vis[N] = { false };
int level[N] = { };
void DFS(int u)
{
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (vis[v] == false)
{
vis[v] = true;
DFS(v);
}
}
}
int BFS(int root)
{
int mlevel = ;
queue<int>q;
q.push(root);
vis[root] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (vis[v] == false)
{
q.push(v);
vis[v] = true;
level[v] = level[u] + ;
if (level[v] > mlevel)
mlevel = level[v];
}
}
}
return mlevel;
}
int main()
{
cin >> n;
int i;
for (i = ; i < n - ; i++)
{
int t1, t2;
cin >> t1 >> t2;
G[t1].push_back(t2);
G[t2].push_back(t1);
}
int tu = ;
for (i = ; i <= n; i++)
{
if (vis[i] == false)
{
tu++;
vis[i] == true;
DFS(i);
}
}
if (tu > )
{
cout << "Error: " << tu << " components";
return ;
}
int maxLevel = ;
vector<int>ans;
for (i = ; i <= n; i++)
{
fill(vis, vis + N, false);
fill(level, level + N, false);
int ceng = BFS(i);
if (ceng == maxLevel)
{
ans.push_back(i);
}
if (ceng > maxLevel)
{
ans.clear();
ans.push_back(i);
maxLevel = ceng;
}
}
for (i = ; i < ans.size(); i++)
cout << ans[i] << endl; return ;
}
方法二:
#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;
#define N 10002
vector<int>G[N];
int n;
bool vis[N] = { false };
int level[N] = { };
void DFS(int u)
{
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (vis[v] == false)
{
vis[v] = true;
DFS(v);
}
}
}
int BFS(int root)
{
int mlevel = ;
queue<int>q;
q.push(root);
vis[root] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (vis[v] == false)
{
q.push(v);
vis[v] = true;
level[v] = level[u] + ;
if (level[v] > mlevel)
mlevel = level[v];
}
}
}
return mlevel;
}
int main()
{
cin >> n;
int i;
for (i = ; i < n - ; i++)
{
int t1, t2;
cin >> t1 >> t2;
G[t1].push_back(t2);
G[t2].push_back(t1);
}
int tu = ;
for (i = ; i <= n; i++)
{
if (vis[i] == false)
{
tu++;
vis[i] = true;
DFS(i);
}
}
if (tu > )
{
cout << "Error: " << tu << " components";
return ;
} fill(vis, vis + N, false);
fill(level, level + N, );
int maxlevel = BFS();
set<int>ans;
for (i = ; i <= n; i++)
if (level[i] == maxlevel)
ans.insert(i);
set<int>::iterator it = ans.begin();
int v = *it;
fill(vis, vis + N, false);
fill(level, level + N, );
maxlevel = BFS(v);
for (i = ; i <= n; i++)
if (level[i] == maxlevel)
ans.insert(i);
for (it = ans.begin(); it != ans.end(); it++)
cout << *it << endl; return ;
}
[PAT] A1021 Deepest Root的更多相关文章
- PAT A1021 Deepest Root (25 分)——图的BFS,DFS
A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...
- 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 Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...
- 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 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- PAT_A1021#Deepest Root
Source: PAT A1021 Deepest Root (25 分) Description: A graph which is connected and acyclic can be con ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
随机推荐
- 初入机器学习,安装tensorflow包等问题总结
学习python,机器学习(maching-lerning).深度学习(deep-learning)等概念也是耳熟能详.我最近从新手开始学习maching-learning知识,不过课程偏向基本的理论 ...
- 移植freertos到stm32 f103 的基本流程和总结
为什么要在stm32 f103上面移植freertos stm32 f103 以他的全面的文档,亲民的价格,强大的功能.成为无数微设备的方案首选.在市场上有极大的使用量.市场占有率也是非常的高.f ...
- 曹工说Spring Boot源码(20)-- 码网灰灰,疏而不漏,如何记录Spring RedisTemplate每次操作日志
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- JavaScript节流与防抖函数封装
js节流与防抖函数封装 常见应用场景: window的 resize 和 scroll 事件: 文字输入时的 keyup 事件: 元素拖拽.移动时的 mousemove 事件: 防抖 定义:多次触发事 ...
- 学会这一招,小白也能使用数据可视化BI软件创建医院数据实时展示大屏
灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件. 本文以医院数据实时展示大屏为例 ...
- 浅谈centos8与centos7
距离centos8.0(现在已经更新到8.1了)的发布已经过去几个月了,作为一个刚刚接触过几个月centos的萌新来说,本文想通过实际的操作体验来说对比一下centos8代与7代 首先,centos8 ...
- uniapp-使用心得
<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v- ...
- pip 自己的源 搭建
1 安装工具 pip install pip2pi 2 下载 所需要的包 pip2tgz /application/nginx/html/yum/python/ apscheduler (172 ...
- SpringBoot整合NoSql--(四)Session共享
简介: 正常情况下,HttpSession是通过Servlet 容器创建并进行管理的,创建成功之后都是保存在内存中.如果开发者需要对项目进行横向扩展搭建集群,那么可以利用一些硬件或者软件工具来做负载均 ...
- watch实现监听Vuex状态监听(利用computed)
Vuex 通过 store 选项,提供了一种机制将状态从根组件"注入"到每一个子组件中(需调用 Vue.use(Vuex)):通过在根实例中注册 store 选项,该 store ...