图不一定联通,所以用并查集找各个联通块的祖先分别建图,之后就和LCA的步骤差不多了 #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<algorithm> #include<vector> using namespace std; inline int read(){ int sum=0,x=1; char ch=getc…
Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4425    Accepted Submission(s): 1263 Problem Description After World War X, a lot of cities have been seriously damag…
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N(N<10000),由C次询问(C<1000000),每次询问两个点,如果来联通输出,两点之间的距离,如果不来联通,输出"Not connected" &题解: md,就没人吐槽这题询问时有相等的情况吗?我被这个坑了一天= = 最后把ans数组置为-1才过的,以前一直初…
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description   After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However,…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路.然后接下来q行,每行给你两个点,问你这两个点的最短距离是多少,要是不相连,则输出一串英文. 首先想到的是用(二分)倍增LCA,但是这题的坑点是两个点可能不在同一个图中,所以我dfs的时候用block[i]标记这个点属于哪一个图中,要是这个点在同一个图中,答案就是cost[u] + cost[v]…
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.…
题意:n棵树,求任意两点的最短距离. 解题关键:并查集判断两点是否位于一棵树上,然后求最短距离即可.此题可以直接对全部区间直接进行st表,因为first数组会将连接的两点的区间表示出来. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib&…
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i) #define rep(i,a,b) for(int i(a); i <= (b); ++i) #define dec(i,a,b) for(int i(a); i >= (b); --i) #define for…
Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some mat…
Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11964    Accepted Submission(s): 2786 Problem Description After World War X, a lot of cities have been seriously damag…