Connections between cities(LCA+并查集)

该博客讨论了一道编程竞赛题目,要求在受损城市间重建道路并找出任意两城市间的最短路径。题目描述了输入格式和输出要求,并给出样例输入和输出。解决方案涉及利用LCA(最近公共祖先)算法和并查集来判断城市是否在同一连通分量中,以及计算最短路径。作者提到在实现过程中遇到的问题,即需要将最大城市数量的限制扩大两倍才能正确运行代码。

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2874

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15139    Accepted Submission(s): 3449

Problem Description

After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.

Input

Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.

Output

For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the leng

### 并查集算法实现集合问题 以下是基于 JavaScript 的并查集算法代码示例,该代码可以用来解决与集合分组、合并和查询相关的问题: #### 初始化并查集类 ```javascript class UnionFind { constructor(n) { this.parent = Array.from({ length: n }, (_, i) => i); // 初始时每个节点的父节点都是自己 this.rank = Array(n).fill(0); // 用于优化树的高度 } find(x) { // 查找操作:找到 x 所属集合的根节点,并进行路径压缩 if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(x, y) { // 合并操作:将两个元素所在的集合合并 let rootX = this.find(x); let rootY = this.find(y); if (rootX === rootY) return; // 如果已经在同一个集合中,则无需处理 if (this.rank[rootX] > this.rank[rootY]) { this.parent[rootY] = rootX; } else if (this.rank[rootX] < this.rank[rootY]) { this.parent[rootX] = rootY; } else { this.parent[rootY] = rootX; this.rank[rootX]++; } } countSets() { // 统计当前有多少个独立的集合 const roots = new Set(); for (let i = 0; i < this.parent.length; i++) { roots.add(this.find(i)); } return roots.size; } } ``` 上述代码实现了并查集的核心功能 `find` 和 `union` 方法。其中 `find` 方法负责查找某个元素所属集合的代表元(即根节点),并通过路径压缩优化性能;而 `union` 方法则负责将两个不同集合中的元素合并成一个集合。 #### 使用场景举例 假设我们需要解决一个问题:给定一组城市之间的连接关系,判断这些城市被划分为多少个连通区域。可以通过如下方式调用上面定义好的 `UnionFind` 类来解决问题[^2]: ```javascript function connectedCities(connections, numCities) { const uf = new UnionFind(numCities); // 将所有相连的城市加入同一集合 connections.forEach(([cityA, cityB]) => { uf.union(cityA - 1, cityB - 1); // 假设城市编号从1开始,因此减去1适配数组索引 }); // 返回最终的集合数量 return uf.countSets(); } // 测试数据 const citiesConnections = [ [1, 2], [2, 3], [4, 5] ]; console.log(connectedCities(citiesConnections, 5)); // 输出应为 2 ``` 此函数接收两参数——城市的连接列表以及总共有几个城市。它会创建一个新的并查集实例并将每一对已知相互连接的城市放入相同的集合里。最后计算剩余的不同集合数目作为结果返回。 ### 总结 通过以上方法能够有效利用并查集这一高效工具快速求解涉及动态变化集合归属情况下的各类复杂度较高的组合型难题[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值