This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathKruskals.cpp
More file actions
62 lines (59 loc) · 1.35 KB
/
Kruskals.cpp
File metadata and controls
62 lines (59 loc) · 1.35 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
#include <bits/stdc++.h>
using namespace std;
struct edge{
int src,dest,wt;
};
bool cmp(edge e1,edge e2)
{
return e1.wt < e2.wt;
}
int getParent(int v,int parent[]){
if(parent[v] == v)
return v;
return getParent(parent[v],parent);
}
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 edges[E];
for(int i=0;i<E;i++)
{
cin >> edges[i].src >> edges[i].dest >> edges[i].wt;
}
sort(edges,edges+E,cmp);
edge output[V-1];
int k=0;
int count = 0;
int parent[V];
for(int i=0;i<V;i++)
{
parent[i] = i;
}
for(int i=0;i<E && count < V-1;i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
int srcParent = getParent(src,parent);
int destParent = getParent(dest,parent);
if(srcParent != destParent)
{
parent[srcParent] = destParent;
count++;
output[k++] = edges[i];
}
}
for(int i=0;i<count;i++)
{
if(output[i].src < output[i].dest)
cout << output[i].src << " " << output[i].dest << " " << output[i].wt << endl;
else
cout << output[i].dest << " " << output[i].src << " " << output[i].wt << endl;
}
return 0;
}