-
Notifications
You must be signed in to change notification settings - Fork 1
/
kru_dsu.cpp
57 lines (54 loc) · 1.36 KB
/
kru_dsu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<int> vi;
vector< pair<int, ii> > EdgeList;
int V,E;
class UnionFind {
private: vi p, rank;
public:
UnionFind(int N) {
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++) p[i] = i;
}
int findSet(int i){
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) p[y] = x;
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
}
};
int Kruskal(){
int mst_cost = 0;
UnionFind UF(V);
for (int i = 0; i < E; i++) {
pair<int, ii> front = EdgeList[i];
if (!UF.isSameSet(front.second.first, front.second.second)) {
mst_cost += front.first;
UF.unionSet(front.second.first, front.second.second);
}
}
return mst_cost;
}
int main(){
cin>>V>>E;
int u,v,w;
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &u, &v, &w);
EdgeList.push_back(make_pair(w, ii(u, v)));
}
sort(EdgeList.begin(), EdgeList.end());
cout<<Kruskal()<<endl;
return 0;
}