-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilding.cpp
More file actions
107 lines (95 loc) · 1.85 KB
/
Building.cpp
File metadata and controls
107 lines (95 loc) · 1.85 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
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
/**
* Author: Dabananda Mitra
* Email: imdmitra@gmail.com
* GitHub: https://github.com/dabananda
* Created: 2024-09-27 19:19:28
* Problem Link: https://www.hackerrank.com/contests/assignment-02-a-introduction-to-algorithms-a-batch-05/challenges/building-3-1
*
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<int, int>
#define pb push_back
#define nl "\n"
const int N = 1e5 + 5;
int leader[N];
int groupSize[N];
class Edge {
public:
int u, v, w;
Edge(int u, int v, int w) {
this->u = u;
this->v = v;
this->w = w;
}
};
void dsuInit(int n) {
for (int i = 1; i <= n; i++) {
leader[i] = -1;
groupSize[i] = 1;
}
}
int getLeader(int node) {
if (leader[node] == -1) {
return node;
}
int l = getLeader(leader[node]);
leader[node] = l;
return l;
}
void makeUnion(int x, int y) {
int a = getLeader(x);
int b = getLeader(y);
if (groupSize[a] > groupSize[b]) {
leader[b] = a;
groupSize[a] += groupSize[b];
} else {
leader[a] = b;
groupSize[b] += groupSize[a];
}
}
bool comp(Edge a, Edge b) {
return a.w < b.w;
}
void solution() {
int n, e;
cin >> n >> e;
vector<Edge> edgeList;
while (e--) {
int u, v, w;
cin >> u >> v >> w;
edgeList.push_back(Edge(u, v, w));
}
sort(edgeList.begin(), edgeList.end(), comp);
long long int cost = 0;
dsuInit(n);
for (Edge ed : edgeList) {
int a = getLeader(ed.u);
int b = getLeader(ed.v);
if (a != b) {
makeUnion(a, b);
cost += ed.w;
}
}
int mainLeader = getLeader(1);
int flag = true;
for (int i = 2; i <= n; i++) {
if (getLeader(i) != mainLeader) {
flag = false;
break;
}
}
if (flag) {
cout << cost << endl;
} else {
cout << -1 << endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solution();
return 0;
}