forked from NJACKWinterOfCode/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskal's algorithm.cpp
58 lines (53 loc) · 1.1 KB
/
Kruskal's algorithm.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
58
// This program uses the Kruskal's algorithm to find out the minimum spanning tree for any connected weighted simple graph.
#include<bits/stdc++.h>
using namespace std;
int findset(int p[], int x){
if(x!=p[x]){
p[x]=findset(p,p[x]);
}
return p[x];
}
void unionset(int p[],int r[],int a,int b){
int x=findset(p,a);
int y=findset(p,b);
if(x!=y){
if(r[x]<r[y]) swap(x,y);
p[y]=x;
if(r[x]==r[y]){
r[x]++;
}
}
}
int main()
{
int n,m;
cin>>n>>m;
int x,y,w;
vector<pair<int,pair<int,int>>> E;
for(int i=0;i<m;i++)
{
cin>>x>>y>>w;
E.push_back({w,{x-1,y-1}});
}
sort(E.begin(),E.end());
int p[n],r[n];
for(int i=0;i<n;i++)
{
p[i]=i;
r[i]=0;
}
vector<int> v;
for(int i=0;i<m;i++)
{
x=findset(p,E[i].second.first);
y=findset(p,E[i].second.second);
if(x!=y)
{
v.push_back(i);
unionset(p,r,E[i].second.first,E[i].second.second);
}
}
for(int i=0;i<v.size();i++)
cout<<E[v[i]].second.first+1<<' '<<E[v[i]].second.second+1<<endl;
return 0;
}