-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskal.cpp
75 lines (70 loc) · 1.41 KB
/
Kruskal.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
class edge{
public:
int v;
int e;
int w;
};
bool com(edge e1,edge e2){
if(e1.w==e2.w){
if(e1.v==e2.v){
return e1.e<e2.e;
}
return e1.v<e2.v;
}
return e1.w<e2.w;
}
void print(edge*arr,int n){
for(int i=0;i<n;i++){
cout<<arr[i].v<<" "<<arr[i].e<<" "<<arr[i].w<<endl;
}
}
int findparent(int parent[],int i){
if(parent[i]!=i){
parent[i]=findparent(parent,parent[i]);
}
return parent[i];
}
int main()
{
int V, E, tempX, tempY;
cin >> V >> E;
/*
Write Your Code Here
Complete the Rest of the Program
You have to Print the output yourself
*/
edge*arr=new edge[E];
for(int i=0;i<E;i++){
int v,e,w;
cin>>v>>e>>w;
arr[i].v=v;
arr[i].e=e;
arr[i].w=w;
}
sort(arr,arr+E,com);
edge*output=new edge[V-1];
int parent[V];
for(int i=0;i<V;i++){
parent[i]=i;
}
int count=0,i=0;
while(count<V-1){
int x=findparent(parent,arr[i].v);
int y=findparent(parent,arr[i].e);
if(x==y){
i++;
}
else{
output[count].v=min(arr[i].e,arr[i].v);
output[count].e=max(arr[i].e,arr[i].v);
output[count].w=arr[i].w;
parent[x]=y;
count++;
i++;
}
}
print(output,V-1);
return 0;
}