当前位置: 首页 > news >正文

宠物用品网站建设网站续费自己做

宠物用品网站建设,网站续费自己做,在线教育网站开发,wordpress播放网盘视频原题 1021 Deepest Root - PAT (Advanced Level) Practice 题目大意 给定一个连通且无环的图(即树),树的高度取决于根节点的选择。请找出能使树的高度最大的所有根节点(称为“最深根”)。若给定的图不是树&#xff…

原题

1021 Deepest Root - PAT (Advanced Level) Practice

题目大意

给定一个连通且无环的图(即树),树的高度取决于根节点的选择。请找出能使树的高度最大的所有根节点(称为“最深根”)。若给定的图不是树(即不连通),需输出连通分量的数量。

解题思路

先找连通分量的数量,利用bfs遍历所有点,标记已经遍历的点,调用函数bfs的次数就是连通分量的个数。

若为树,利用两次bfs和无序集合unordered_set来保存使树深度最大的点,只用一次bfs有可能遇到如图情况:假设我们从G点开始遍历,M点就不会进入答案,因此我们先遍历一次,找到最远的为B,再从B开始遍历,找到M。

代码(c++)

#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_set>using namespace std;const int N = 10010;int n;
vector<vector<int>> graph(N);          // 模拟邻接表
vector<bool> visited(N, false);vector<int> bfs(int start, const vector<vector<int>>& graph, int n) {vector<int> depth(n + 1, -1);      // 记录每个点的深度queue<int> q;q.push(start);depth[start] = 0;int max_depth = 0;                 // 动态记录最深的深度while (!q.empty()) {int u = q.front();q.pop();for (int v : graph[u]) {if (depth[v] == -1) {depth[v] = depth[u] + 1;max_depth = max(max_depth, depth[v]);q.push(v);}}}vector<int> res;for (int i = 1; i <= n; ++i) {if (depth[i] == max_depth) {res.push_back(i);}}return res;
}int main() {cin >> n;for(int i = 0; i < n - 1; i++) {int u, v;cin >> u >> v;graph[u].push_back(v);graph[v].push_back(u);}int components = 0;for (int i = 1; i <= n; ++i) {if (!visited[i]) {components++;queue<int> q;q.push(i);visited[i] = true;while (!q.empty()) {int u = q.front();q.pop();for (int v : graph[u]) {if (!visited[v]) {visited[v] = true;q.push(v);}}}}}if(components == 1) {// 两次遍历找到所有最深的点vector<int> Y = bfs(1, graph, n);                vector<int> Z = bfs(Y[0], graph, n);unordered_set<int> deepest;for (int y : Y) deepest.insert(y);for (int z : Z) deepest.insert(z);vector<int> ans(deepest.begin(), deepest.end());sort(ans.begin(), ans.end());for (int node : ans) {cout << node << endl;}}else cout << "Error: "<< components << " components";
}

http://www.dtcms.com/a/615817.html

相关文章:

  • 课程设计代做网站php制作网页怎么制作
  • 网站广告联盟平台如何建设网站兴田德润可信赖
  • 聊城做网站费用价位wordpress模版安装
  • 示范校建设验收网站不重名的建筑公司名字
  • 网站 开发逻辑个人网站能挂广告吗
  • 贵阳市城乡建设厅网站网站开发后台指什么
  • 一个网站页面设计多少钱教人做美食的网站
  • 钢管网站建设网站百度知道
  • 公司网站建设一定要求原图吗wordpress 下单
  • 网站更新的意义个人网站放什么内容
  • 查找使用wordpress的网站十大广告联盟
  • 基于tensorflow框架的MSCNN-LSTM模型在CWRU轴承故障诊断的应用
  • 花卉物流园做网站的素材修改 自豪地采用wordpress
  • 保定网站建设团队账号注册平台
  • 上海市住房建设部官方网站个体工商户做网站
  • (论文速读)具有深度引导交叉视图一致性的3D高斯图像绘制
  • vps挂网站做淘宝美工的网站
  • 网站建设项目报价单网站建设企业官网源码
  • 网址查询网站深圳seo优化排名公司
  • it 网站模板造价员建设部网站
  • 淘宝客网站怎么建设qq引流推广软件哪个好
  • php做网站难么在线看私人不收费不登录
  • 青山做网站红色风格网站
  • 网站开发项目需求分析说明书网页设计及网站建设在线作业
  • 天津南昌网站建设濮阳网络直播
  • 专业的团队网站建设html模板在哪找
  • python django做网页企业关键词优化价格
  • 可以做免费的网站吗手机网页设计app
  • 单页面网站有哪些内容网络销售公司经营范围
  • 独立看门狗(IWDG)