-
Notifications
You must be signed in to change notification settings - Fork 0
/
1197.cpp
66 lines (50 loc) · 911 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
using namespace std;
struct cost
{
int a, b;
int c;
bool operator<(struct cost k)
{
return k.c > c;
}
}cost[100001];
int parent[10001];
long long int sum;
int set_find(int);
void set_union(int, int,int);
int main()
{
ios_base::sync_with_stdio(false);
long long int v, e;
cin >> v >> e;
for (int i = 1; i <= v; i++)
parent[i] = i;
for (int i = 1; i <= e; i++)
cin >> cost[i].a >> cost[i].b >> cost[i].c;
sort(cost + 1, cost + 1 + e);
for (int i = 1; i <= e; i++)
set_union(cost[i].a, cost[i].b,i);
cout << sum << endl;
return 0;
}
int set_find(int x)
{
if (parent[x] != x)
parent[x] = set_find(parent[x]);
return parent[x];
}
void set_union(int x, int y, int i)
{
int xroot, yroot;
xroot = set_find(x);
yroot = set_find(y);
if (xroot == yroot)
return;
else
{
parent[yroot] = xroot;
sum += cost[i].c;
}
}