-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal's MST Algorithm.cpp
More file actions
108 lines (89 loc) · 1.66 KB
/
Kruskal's MST Algorithm.cpp
File metadata and controls
108 lines (89 loc) · 1.66 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Kruskal's MST Algorithm
Time Complexity: O(ElogV)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>rep;
vector<int>sz;
void make_set(int x)
{
rep[x] = x;
sz[x] = 1;
}
int find_set(int x)
{
while(rep[x] != x)
{
rep[x] = rep[rep[x]];
x = rep[x];
}
return x;
}
void union_set(int x, int y)
{
int a = find_set(x);
int b = find_set(y);
if(a!=b)
{
if(sz[a] < sz[b])
{
rep[b] = a;
sz[a] += sz[b];
}
else
{
rep[a] = b;
sz[b] += sz[a];
}
}
}
bool mysort(pair<pair<int,int>,int> &A, pair<pair<int,int>,int> &B)
{
return A.second<B.second;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,M;
cin>>N>>M;
vector<pair<pair<int,int>,int>> edges;
map<pair<int,int>,int>MAP;
rep.resize(N+1);
sz.resize(N+1);
for(int m = 0; m<M; m++)
{
int u,v,w;
cin>>u>>v>>w;
if(MAP.find({u,v}) == MAP.end())
MAP[{u,v}] = w;
else
MAP[{u,v}] = min(MAP[{u,v}], w);
}
for(auto it: MAP)
{
int u = it.first.first;
int v = it.first.second;
int w = it.second;
edges.push_back({{u,v},w});
}
for(int n = 0; n<=N; n++)
make_set(n);
sort(edges.begin(), edges.end(), mysort);
int sum = 0;
for(auto it: edges)
{
int u = it.first.first;
int v = it.first.second;
int w = it.second;
if(find_set(u) != find_set(v))
{
sum += w;
union_set(u,v);
}
}
cout<<sum<<endl;
return 0;
}