HDU-2874Connections between cities(lca模板题,注意是森林)
题目大意
题目链接
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 length of the shortest path between them.
Sample Input
1 | 5 3 2 |
Sample Output
1 | Not connected |
分析
在森林中求lca。
用并查集判断两个点是不是在同一棵树中。
用tarjan离线一直mle。。。无语了, 最后用的倍增才过的。
第一次遇到用时间换空间的题。。。
倍增ac代码
1 |
|
tarjan MLE代码
1 |
|
1 | 恰似你一低头的温柔,娇弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。 --mingfuyan |
原文作者: Mingfu Yan
原文链接: https://solodance.top/2020/06/26/HDU-2874Connections between cities(lca模板题,注意是森林)/
版权声明: 转载请注明出处(必须保留作者署名及链接)