forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kruskalsAlgorithm.cpp
60 lines (51 loc) ยท 1.12 KB
/
kruskalsAlgorithm.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
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long int ll;
ll find(vector<ll>&p, ll v){
if(p[v]==v)
return v;
return p[v] = find(p, p[v]);
}
bool C(vector<ll>& p, vector<ll> &r, ll v1, ll v2){
ll p1 = find(p, v1);
ll p2 = find(p, v2);
if(p1==p2) return 0;
if(r[p1]==r[p2]){
p[p2] = p1;
r[p1]++;
}
else if(r[p1]>r[p2])
p[p2] = p1;
else
p[p1] = p2;
return 1;
}
int main(){
// write your code here
ll t;
cin>>t;
while(t--){
ll v,e;
cin>>v>>e;
vector<pair<ll, pair<ll, ll>>> gr(e);
for(ll i=0 ; i<e ; i++){
cin>>gr[i].S.F>>gr[i].S.S>>gr[i].F;
}
sort(gr.begin(), gr.end());
vector<ll> p(v);
vector<ll> r(v, 1);
for(int i=0 ; i<v ; i++)
p[i]= i;
ll w=0, c=0;
for(ll i=0; i<e && c<v-1; i++ ){
if(C(p, r, gr[i].S.F, gr[i].S.S)){
w+=gr[i].F;
c++;
}
}
cout<<w<<"\n";
}
return 0;
}