-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1675.cc
88 lines (77 loc) · 1.71 KB
/
1675.cc
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
#include <bits/stdc++.h>
using namespace std;
namespace graph {
/**
* Disjoint Set Union is used to solve problems where edge(u,v) denote union of
* set u and set v.
*
* Build time : O(N)
* AddEdge : O(log* N)
* GetParent : O(log* N)
* GetSize : O(log* N)
*
* Author: Himanshu Jaju
*/
class DisjointSetUnion {
public:
DisjointSetUnion(int N) : N_(N) {
par_.resize(N_ + 1);
size_.resize(N_ + 1);
for (int i = 0; i <= N_; i++) {
size_[i] = 1;
par_[i] = i;
}
}
// Returns the parent of |x|'s set.
int GetParent(int x) {
if (par_[x] != x) {
par_[x] = GetParent(par_[x]);
}
return par_[x];
}
// Returns the size of set in which |x| belongs.
int GetSize(int x) {
return size_[GetParent(x)];
}
// Add an edge between |u| and |v|. Creates union of both sets.
void AddEdge(int u, int v) {
int parent_u = GetParent(u);
int parent_v = GetParent(v);
if (parent_u != parent_v) {
if (size_[parent_v] > size_[parent_u]) {
swap(parent_u, parent_v);
}
par_[parent_v] = parent_u;
size_[parent_u] += size_[parent_v];
size_[parent_v] = 0;
}
}
private:
int N_;
vector<int> par_, size_;
};
} // namespace graph
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, pair<int,int>>> arr(m);
for (auto &x: arr) {
cin >> x.second.first >> x.second.second >> x.first;
}
sort(arr.begin(), arr.end());
graph::DisjointSetUnion dsu(n);
long long ans = 0;
for (auto x: arr) {
int u = x.second.first;
int v = x.second.second;
int c = x.first;
if (dsu.GetParent(u) == dsu.GetParent(v)) continue;
ans += c;
dsu.AddEdge(u, v);
}
if (dsu.GetSize(1) != n)
cout << "IMPOSSIBLE\n";
else
cout << ans << "\n";
return 0;
}