forked from atyant-yadav/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst using kruskal .cpp
More file actions
59 lines (59 loc) · 1.08 KB
/
mst using kruskal .cpp
File metadata and controls
59 lines (59 loc) · 1.08 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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
#define mod 1000000007
vi v[10005], p(10005);
int n, m;
int find(int x)
{
if (x == p[x])
return x;
return find(p[x]);
}
void union1(int x, int y)
{
int p1 = find(x), p2 = find(y);
if (p1 == p2)
return;
p[p2] = p1;
}
void init()
{
for (int i = 1; i <= n; i++)
p[i] = i;
}
int kruskal(vector<pair<int, pair<int, int>>> edges)
{
int cost, x, y, t_cost = 0;
for (int i = 0; i < edges.size(); i++)
{
x = edges[i].s.f;
y = edges[i].s.s;
cost = edges[i].f;
if (find(x) != find(y))
{
union1(x, y);
t_cost += cost;
}
}
return t_cost;
}
int32_t main()
{
int a, b, w;
vector<pair<int, pair<int, int>>> edges;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> w;
edges.pb({w, {a, b}});
}
init();
sort(edges.begin(), edges.end());
cout << kruskal(edges);
return 0;
}