public ConnectingGraph3(int n) { // initialize your data structure here. father = new int[n + 1]; for (int i = 0; i < n + 1; i++){ father[i] = i; } count = n; } public void connect(int a, int b) { // Write your code here int root_a = find(a); int root_b...
原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/ 题目: There areNcities numbered from 1 toN. You are givenconnections, where eachconnections[i] = [city1, city2, cost]represents the cost to connectcity1andcity2together. (Aconnectionis bidirectional: connectingcit...
一个union函数,一个find函数,哦了。 https://leetcode.com/problems/connecting-cities-with-minimum-cost/discuss/344867/Java-Kruskal's-Minimum-Spanning-Tree-Algorithm-with-Union-Find classSolution {int[] parent;intn;privatevoidunion(intx,inty) {intpx =find(x);intpy =find(y);if(px !=py) { ...