forked from hacktoberfest2k20/DataStructures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskal's Algorithm.cpp
149 lines (128 loc) · 2.92 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and
E number of edges.Find and print the Minimum Spanning Tree (MST) using Kruskal's algorithm.*/
#include <iostream>
#include <string.h>
#include <utility>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <math.h>
#define pp pair<int, int>
#define vpp vector<pp>
#define vecto vector<ll>
#define ll long long
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define ull unsigned long long int
#define f(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
ll vert[100010];
ll parent[100010];
vector<vector<pp>> v(100010);
vector<pair<ll, pp>> ans;
bool sortby(pair<ll, pp> &a, pair<ll, pp> &b)
{
return a.ff < b.ff;
}
ll Find_parent(ll a)
{
if (parent[a] != a)
{
return Find_parent(parent[a]);
}
else
{
return a;
}
}
void kruskal(ll vertices, queue<pair<ll, pp>> q, ll &count, ll &sum)
{
if (count >= vertices)
return;
if (q.empty())
return;
pair<ll, pp> top = q.front();
q.pop();
ll parent1 = Find_parent(top.ss.ss);
ll parent2 = Find_parent(top.ss.ff);
if (parent1 == parent2)
{
vert[top.ss.ff] = 1;
vert[top.ss.ss] = 1;
count++;
}
else
{
vert[top.ss.ff] = 1;
vert[top.ss.ss] = 1;
parent[parent1] = parent2;
count++;
ans.pb(top);
//parent[top.ss.ff] = top.ss.ss;
}
/*
for (int i = 0; i < vertices; i++)
{
cout << parent[i] << " ";
}
cout << "\n";
for (int i = 0; i < vertices; i++)
{
cout << vert[i] << ' ';
}
cout << "\n";
*/
kruskal(vertices, q, count, sum);
return;
}
void solve()
{
ll vertices, e;
cin >> vertices >> e;
memset(vert, 0, sizeof(vert));
for (int i = 0; i <= 100001; i++)
{
parent[i] = i;
}
vector<pair<ll, pp>> order;
for (int i = 0; i < e; i++)
{
ll a, b, c;
cin >> a >> b >> c;
v[a].pb(mp(b, c));
v[b].pb(mp(a, c));
order.pb(mp(c, mp(a, b)));
}
sort(order.begin(), order.end(), sortby);
queue<pair<ll, pp>> q;
for (int i = 0; i < order.size(); i++)
{
q.push({order[i].ff, {order[i].ss.ff, order[i].ss.ss}});
}
ll count = 0, sum = 0;
kruskal(vertices, q, count, sum);
for (int i = 0; i < ans.size(); i++)
{
if (ans[i].ss.ff <= ans[i].ss.ss)
cout << ans[i].ss.ff << " " << ans[i].ss.ss << " " << ans[i].ff << "\n";
else
{
cout << ans[i].ss.ss << " " << ans[i].ss.ff << " " << ans[i].ff << "\n";
}
}
}
int main()
{
IOS;
solve();
}