forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1675 - Road Reparation.cpp
More file actions
67 lines (61 loc) · 1.25 KB
/
1675 - Road Reparation.cpp
File metadata and controls
67 lines (61 loc) · 1.25 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
// Road Reparation
//
// Problem name: Road Reparation
// Problem Link: https://cses.fi/problemset/task/1675
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 200100
#define INF 1000000001
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;
int pai[MAXN], peso[MAXN];
vector<pair<int, pii>> v;
int find(int a) {
if (pai[a] == a) return a;
return pai[a] = find(pai[a]);
}
void join(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (peso[a] > peso[b]) {
pai[b] = a;
}
if (peso[b] > peso[a]) {
pai[a] = b;
}
if (peso[a] == peso[b]) {
pai[a] = b;
peso[b]++;
}
return;
}
int main () { _
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) pai[i] = i;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
v.pb({c, {a, b}});
}
sort(v.begin(), v.end());
ll ans = 0;
int count = 0;
for (int i = 0; i < m; i++) {
if (find(v[i].S.F) != find(v[i].S.S)) {
join(v[i].S.F, v[i].S.S);
ans += v[i].F;
count++;
}
}
if (count == n-1) cout << ans << '\n';
else cout << "IMPOSSIBLE\n";
return 0;
}