-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1675.cpp
More file actions
47 lines (42 loc) · 873 Bytes
/
cses_1675.cpp
File metadata and controls
47 lines (42 loc) · 873 Bytes
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
#include <bits/extc++.h>
using namespace std;
vector<int> parent;
int find(int x)
{
return parent[x] == x ? x : parent[x] = find(parent[x]);
}
void un(int x, int y)
{
parent[find(x)] = parent[find(y)];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, cnt = 0;
int64_t cost = 0;
cin >> n >> m;
parent.resize(n + 1);
for (int i = 0; i <= n; i++)
parent[i] = i;
vector<tuple<int, int, int>> edge(m);
for (auto &[w, u, v] : edge)
cin >> u >> v >> w;
sort(edge.begin(), edge.end());
for (auto const [w, u, v] : edge)
{
if (find(u) == find(v))
continue;
else
{
un(u, v);
cost += w;
cnt++;
}
}
if (cnt == n - 1)
cout << cost << '\n';
else
cout << "IMPOSSIBLE\n";
return 0;
}