当前位置:网站首页>华为机试:找城市
华为机试:找城市
2022-07-19 21:34:00 【小朱小朱绝不服输】
【编程题目 |200分】 找城市【2022 Q1,Q2 考试题】
题目描述
一个城市规划问题,一个地图有很多城市,两个城市之间只有一种路径,切断通往一个城市i的所有路径之后,其他的城市形成了独立的城市群,这些城市群里最大的城市数量,就是聚集度DPi,现在给出一个地图上各个城市的路径,输出聚集度最小的城市,如果有多个结果,按照编号从小到大
输入描述
第一行输入 城市节点数目N
后面N-1输入城市之间的路径
输出描述
聚集度最小的城市
示例
输入
5
1 2
2 3
3 4
4 5
输出
3
说明
将通往3的所有路径切断,最大城市群数量是2,其他任意城市切断后,最大城市群数量都比2大,所以输出3
输入
6
1 2
2 3
2 4
3 5
3 6
输出
2 3
说明
将通往2或者3的所有路径切断,最大城市群数量是3,其他任意城市切断后,最大城市群数量都比3大,所以输出2 3
思路分析
题目中描述切断通往一个城市i的所有路径之后,其他的城市形成了独立的城市群,这比较容易想到并查集,两个城市相连,就把两个城市合并。
循环n次遍历所有城市的结果,每个城市遍历所有的链接信息,如果出现当前循环需要排除的城市,则不执行本次合并操作。
需要注意的是:
- 并查集模板中初始化时,需要从1开始,因为城市的编号是从1开始。
- 统计每个城市所在的最大的城市数量,即聚集度
并查集方法不要害怕,因为并查集类是有固定模板,主函数一般只需要将相连的两个合并操作即可,最后统计数量。可以参考:【Leetcode】并查集(Union-Find)算法。
参考代码
import java.util.Scanner;
public class jujidu {
static class UF {
// 路径压缩的加权quick-union算法模板
int N;
int count;
int[] id;
int[] sz;
private UF (int n) {
N = n;
count = n;
id = new int[n + 1];
sz = new int[n + 1];
for (int i = 1; i <= n; i++) {
// 这里需要注意,城市是从1开始的
id[i] = i;
sz[i] = 1;
}
}
public int getMax () {
// 统计并查集的最大值
int max = 0;
for (int i = 1; i < sz.length; i++) {
max = Math.max(max, sz[i]);
}
return max;
}
public void union (int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot != qRoot) {
if (sz[pRoot] < sz[qRoot]) {
id[pRoot] = qRoot;
sz[qRoot] += sz[pRoot];
} else {
id[qRoot] = pRoot;
sz[pRoot] += sz[qRoot];
}
count--;
}
}
private int find (int p) {
if (p == id[p]) {
return p;
}
id[p] = find(id[p]);
return id[p];
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n - 1; i++) {
arr[i][0] = in.nextInt();
arr[i][1] = in.nextInt();
}
// 并查集
int res = Integer.MAX_VALUE; // 统计最小聚集度
int[] maxArray = new int[n + 1]; // 统计每个城市的聚集度
for (int i = 1; i <= n; i++) {
// 对于每一个城市
UF uf = new UF(n);
for (int j = 0; j < n - 1; j++) {
// 判断每一条路径
if (arr[j][0] == i || arr[j][1] == i) {
continue;
} else {
uf.union(arr[j][0], arr[j][1]);
}
}
maxArray[i] = uf.getMax(); // 每个城市对应的聚集度
res = Math.min(res, maxArray[i]); // 切掉路径后的最小聚集度
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i < maxArray.length; i++) {
if (maxArray[i] == res) {
sb.append(i).append(" ");
}
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
}
欢迎在评论区指正以及留下自己的更简洁的方法。
边栏推荐
- Operation element case collection
- XML to VOC, VOC to coco, coco to Yolo, coco partition, coco check, Yolo check, coco visualization
- 查缺补漏C语言:字符串处理函数
- Es6-11 learning notes
- uniapp使用Hbuilder X方式, 安装uivew插件步骤
- visual studio 2019 安装卸载问题
- Unity 3D人物的粒子消散
- PyTorch笔记 - R-Drop、Conv2d、3x3+1x1+identity算子融合
- [C debugging] - debug C code using vs 2012 ide environment
- 详解“洋葱架构”
猜你喜欢
es-从搜索中检索选定的字段
7-15 operation
The interviewer asked me why the GC generational collection algorithm of JVM was so designed
7-15作业
Improve the mirror station configuration information - mirror station experience Officer
DNS域名解析
基于SSM实现水果蔬菜商城管理系统
创意:高仿矿质颜料AI油画作品呈现
visual studio 2019 安装卸载问题
Arthas Xiaobai beginner
随机推荐
DNS域名解析服务
详细解读:反射的用法
火爆各平台的拼团功能,宝子们在多商户系统中玩过吗?
TS 学习笔记
(JS) trouver le nombre de paires uniques dans le tableau sans espace auxiliaire
西北工业大学|使用交互式界面的多智能体模型协作
Improve the mirror station configuration information - mirror station experience Officer
整流桥厂家ASEMI有哪些型号,整流桥厂家的电镀工艺怎么样?
(JS)不使用辅助空间找出数组中唯一成对的数
[swoole series 2.5] asynchronous tasks
4R analysis redis processing client requests
页面请求拦截
【Matlab】解决使用Mex 报错There was a problem creating the mex file for Real Time Execution ,Please ensure y
服务器状态码
主流定时任务解决方案全横评
Debezium系列之:优化集群参数和支持debezium connector个性化设置
SQL刷题:找出所有员工当前薪水salary情况
Detailed interpretation: the use of reflection
微生物与我们的生活息息相关
How to publish and subscribe messages with redis? What is the implementation principle?