-
Notifications
You must be signed in to change notification settings - Fork 0
/
1197.cpp
42 lines (41 loc) · 986 Bytes
/
1197.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
#include <bits/stdc++.h>
#define MAXN 10000
using namespace std;
int v, e;
vector<pair<int, pair<int, int>>> g;
int Parent[MAXN+1];
int Find(int x){
if(Parent[x] == x) return x;
else return Parent[x] = Find(Parent[x]);
}
void Union(int x, int y){
x = Find(x);
y = Find(y);
if(x != y) Parent[x] = y;
}
bool SameParent(int x, int y){
x = Find(x);
y = Find(y);
return x==y;
}
int main(void){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> v >> e;
for(int i=0; i<e; i++){
int a, b, w;
cin >> a >> b >> w;
g.push_back({w, {a,b}});
}
sort(g.begin(), g.end());
for(int i=1; i<=v; i++){
Parent[i] = i;
}
int ans = 0;
for(int i=0; i<e; i++){
if(!SameParent(g[i].second.first, g[i].second.second)){
ans += g[i].first;
Union(g[i].second.first, g[i].second.second);
}
}
cout << ans << "\n";
return 0;
}