-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spanning Tree using Kruskal’s Algorithm.cpp
79 lines (70 loc) · 1.62 KB
/
Spanning Tree using 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
// https://www.topcoder.com/community/data-science/data-science-tutorials/disjoint-set-data-structures/
// ^ Very good tutorial for Disjoint Sets Data Structure
// https://www.ics.uci.edu/~eppstein/161/960206.html
// Give it a fast read for proof of Kruskal's Algorithm
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
#define rep(i, N) for(int i=0; i<N; i++)
#define REP(i, N) for(int i=1; i<=N; i++)
#define si(n) scanf("%d",&n)
#define pin(n) printf("%d\n",n)
#define pis(n) printf("%d",n)
#define plln(n) printf("%lld\n",n)
#define plls(n) printf("%lld",n)
#define P pair<int,int>
#define PP pair<P,int>
#define F first
#define S second
typedef long long ll;
int n,m;
PP arr[100001];
bool comp(PP A, PP B){
return (A.S<B.S);
};
int currentRank[10001];
int Parent[10001];
void initializer(){
rep(i,10001) {
currentRank[i]=0;
Parent[i]=i;
};
};
int root(int x){
if(x!=Parent[x]) Parent[x]=root(Parent[x]);
return Parent[x];
};
void union1(int x, int y){
int PX=root(x);
int PY=root(y);
if(currentRank[PX]<currentRank[PY]) Parent[PX]=PY;
else if(currentRank[PX]>currentRank[PY]) Parent[PY]=PX;
else {
currentRank[PX]=currentRank[PY]+1;
Parent[PY]=PX;
};
};
bool checkUnion(int i){
int x=arr[i].F.F;
int y=arr[i].F.S;
if(root(x)!=root(y)){
union1(x,y);
return true;
};
return false;
};
int main()
{
si(n); si(m);
for(int i=0; i<m; i++) cin >> arr[i].F.F >> arr[i].F.S >> arr[i].S ;
sort(arr, arr+m, comp);
initializer();
ll answer=0;
for(int i=0; i<m; i++){
if(checkUnion(i)) {
answer+=arr[i].S;
};
};
cout << answer << endl;
return 0;
}