1021. Deepest Root (25)

news/2025/11/23 17:01:54/文章来源:https://www.cnblogs.com/WJQ2017/p/19260914

1021. Deepest Root (25)

#include <iostream>
#include <queue>
#include <vector>
#include <string.h>using namespace std;int vis[10010], sum[10010], tree[10010], level;
queue<int> q;
vector<int> v[10010];void init(int n)
{int i;for(i = 1; i <= n; i++){tree[i] = -1;sum[i] = 1;}
}int findroot(int root)
{if(tree[root] == -1){return root;}else{tree[root] = findroot(tree[root]);return tree[root];}
}void buildrelation(int index[2])
{int i;for(i = 0; i <= 1; i++){index[i] = findroot(index[i]);}int index0 = index[0], index1 = index[1];if(index0 != index1){if(sum[index1] > sum[index0]){index0 = index[1];index1 = index[0];}tree[index1] = index0;sum[index0] += sum[index1];}
}void bfs()
{int qsize = q.size(), vsize, cur, i, next;level++;while(qsize--){cur = q.front();q.pop();vsize = v[cur].size();for(i = 0; i <= vsize - 1; i++){next = v[cur][i];if(vis[next] == 0){vis[next] = 1;q.push(next);}}}qsize = q.size();if(qsize > 0){bfs();}
}int getrootcount(int n)
{int i, count = 0;for(i = 1; i <= n; i++){if(tree[i] == -1){count++;}}return count;
}int main()
{int n;scanf("%d", &n);init(n);int i, a, b, index[2];for(i = 1; i <= n - 1; i++){scanf("%d%d", &a, &b);v[a].push_back(b);v[b].push_back(a);index[0] = a;index[1] = b;buildrelation(index);}int count = getrootcount(n);if(count > 1){printf("Error: %d components\n", count);return 0;}int max = 0;vector<int> res;for(i = 1; i <= n; i++){memset(vis, 0, sizeof(vis));vis[i] = 1;level = 0;q.push(i);bfs();if(level > max){max = level;res.clear();res.push_back(i);}else if(level == max){res.push_back(i);}}int size = res.size();for(i = 0; i <= size - 1; i++){printf("%d\n", res[i]);}system("pause");return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/974041.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Error: Internal Error: Sub-system: FDI_DATA——Cyclone 10 gx编译报错

Error: Internal Error: Sub-system: FDI_DATA——Cyclone 10 gx编译报错错误提示:Error: Internal Error: Sub-system: FDI_DATA, File: /quartus/ddb/fdi/fdi_timing_model.cpp, Line: 753 Error - could not find …

反悔贪心题目总结

模拟费用流 单向链模型、双向链模型 经典例题 反悔贪心&模拟费用流 - Xun_Xiaoyao 反悔贪心(模拟费用流) - Xttttr CF865D Buy Low Sell High P11457 [USACO24DEC] Job Completion G P4053 [JSOI2007] 建筑抢修 …

1012. The Best Rank (25)

1012. The Best Rank (25)#include <iostream> #include <algorithm>using namespace std;struct node {int id, score[5]; }stu[2010];int flag[1000000], bestrankindex[1000000], cmpindex;int cmp(nod…

1016. Phone Bills (25)

1016. Phone Bills (25)#include <iostream> #include <map> #include <string> #include <string.h> #include <vector> #include <algorithm>using namespace std;struct node …

人工智能之数据分析 numpy:第二章 简介与安装

人工智能之数据分析 numpy:第二章 简介与安装人工智能之数据分析 numpy 第二章 简介与安装@目录人工智能之数据分析 numpy前言一、NumPy 的主要特点二、安装 NumPy方法 1:使用 pip 安装(推荐)方法 2:使用 conda 安…

2025 年 11 月武汉废旧金属回收厂家权威推荐榜:不锈钢/钛钢,模具钢,废铁/废铜/废铝/铝合金,旧电缆,废旧物资及厂房拆除专业服务

2025 年 11 月武汉废旧金属回收厂家权威推荐榜:不锈钢/钛钢,模具钢,废铁/废铜/废铝/铝合金,旧电缆,废旧物资及厂房拆除专业服务 一、行业背景与发展趋势 随着我国循环经济战略的深入推进,废旧金属回收行业正迎来前所…

20232324 2025-2026-1 《网络与系统攻防技术》实验七实验报告

20232324 2025-2026-1 《网络与系统攻防技术》实验七实验报告1.实验内容使用SET工具建立冒名网站; 使用Ettercap进行DNS欺骗; 结合SET与Ettercap技术实施DNS欺骗钓鱼攻击; 提高防范意识,并提出具体防范方法。2.实验…

1009. Product of Polynomials (25)

1009. Product of Polynomials (25)#include "stdafx.h" #include <iostream> #include <map>using namespace std;int main() {int n1;scanf("%d", &n1);int i, a;double b;map&…

1014. Waiting in Line (30)

1014. Waiting in Line (30)#include <iostream> #include <queue>using namespace std;int main() {int n, m, k, q;scanf("%d%d%d%d", &n, &m, &k, &q);int processing[1010]…

2025 年 11 月 AGV 搬运设备厂家权威推荐榜:自动叉车/智能搬运小车/堆高码垛/AMR 潜伏式/仓储物流无人叉车/激光 SLAM 导航/箱式搬运上下料机器人实力解析

2025 年 11 月 AGV 搬运设备厂家权威推荐榜:自动叉车/智能搬运小车/堆高码垛/AMR 潜伏式/仓储物流无人叉车/激光 SLAM 导航/箱式搬运上下料机器人实力解析 随着工业4.0和智能制造的深入推进,AGV搬运设备作为现代物流…

1006. Sign In and Sign Out (25)

1006. Sign In and Sign Out (25)#include "stdafx.h" #include <iostream> #include <string.h>using namespace std;int main() {int n;scanf("%d", &n);int i, j, hour[2], se…

2025 年 11 月东北地区商业秘密保护服务权威推荐榜:覆盖沈阳、北京、吉林、辽宁、长春、黑龙江制造业、高新技术企业、化工企业、中小型企业、上市公司,专业护航企业核心竞争力

2025 年 11 月东北地区商业秘密保护服务权威推荐榜:覆盖沈阳、北京、吉林、辽宁、长春、黑龙江制造业、高新技术企业、化工企业、中小型企业、上市公司,专业护航企业核心竞争力 在当今知识经济时代,商业秘密作为企业…

1010. Radix (25)

1010. Radix (25)#include "stdafx.h" #include <iostream> #include <string.h>using namespace std;long long getnumbychar(char c) {long long num;if(c >= 0 && c <= 9){num…

终章:混天绫与全球治理迷锁——重塑AI时代的文明契约

终章:混天绫与全球治理迷锁——重塑AI时代的文明契约 这尊新生的哪吒,其力量正在穿透国界,撼动全球治理的根基。 它的“混天绫”,是那套透明、可审计、可演化的“AI元人文”架构,在跨境数据流中舞动,令各国孤立的…

1001. A+B Format (20)

1001. A+B Format (20)#include "stdafx.h" #include <iostream> #include <algorithm> #include <queue> #include <stdlib.h> #include <string.h>using namespace std;int…

1002. A+B for Polynomials (25)

1002. A+B for Polynomials (25)#include "stdafx.h" #include <iostream> #include <map>using namespace std;int main() {int n1;scanf("%d", &n1);map<int, double> id…

2025 年 11 月货架厂家权威推荐榜:重型模具/高位阁楼/智能仓储/冷库定制/立体库/AGV机器人/悬臂式/穿梭车密集型全品类深度解析与选购指南

2025 年 11 月货架厂家权威推荐榜:重型模具/高位阁楼/智能仓储/冷库定制/立体库/AGV机器人/悬臂式/穿梭车密集型全品类深度解析与选购指南 行业背景与发展趋势 随着现代工业物流体系的快速升级,仓储货架行业正经历着…

1003. Emergency (25)

1003. Emergency (25)#include "stdafx.h" #include <iostream> #include <vector>using namespace std;struct node {int next, length; };vector<node> v[510]; int team[510], vis[510…

2025 年 11 月冷库板厂家权威推荐榜:聚氨酯冷库板,机制板,冷库门,冷库工程集成服务,高效保温与专业定制之选

2025 年 11 月冷库板厂家权威推荐榜:聚氨酯冷库板,机制板,冷库门,冷库工程集成服务,高效保温与专业定制之选 随着冷链物流行业的快速发展,冷库作为关键基础设施,其保温性能与工程质量直接影响到食品、医药等产品…

数据采集_3

数据采集_3目录第一题:多线程爬取图片核心代码与运行结果:心得体会:第二题:scrapy爬取股票核心代码与运行结果:心得体会:第三题:scrapy爬取外汇网站数据核心代码与运行结果: 第一题:多线程爬取图片 核心代码与运行结果…