-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathP3366.cpp
58 lines (54 loc) · 1.11 KB
/
P3366.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
// Kruskal Algorithm
#include <cstdio>
#include <algorithm>
#define MAXM 200005
#define MAXN 5005
using namespace std;
struct Edge {
int x, y, z;
} Edges[MAXM];
int father[MAXN]; // union-find set
int n, m, ans = 0;
int cmp(Edge &a, Edge &b)
{
return a.z < b.z;
}
int find(int a)
{
if (father[a] == a) return a;
else
{
father[a] = find(father[a]);
return father[a];
}
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 1; i <= m; i++) scanf("%d %d %d", &Edges[i].x, &Edges[i].y, &Edges[i].z);
sort(Edges+1, Edges+1+m, cmp);
for (int i = 1; i <= m; i++)
{
int u = Edges[i].x, v = Edges[i].y,
fu = find(u), fv = find(v);
if (fu != fv)
{
father[fu] = fv;
ans += Edges[i].z;
}
}
bool has_answer = true;
int f = find(1);
for (int i = 2; i <= n; i++)
if (find(i) != f)
{
has_answer = false;
break;
}
if (has_answer)
printf("%d\n", ans);
else
printf("orz\n");
return 0;
}