-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20170304.cpp
More file actions
69 lines (62 loc) · 893 Bytes
/
20170304.cpp
File metadata and controls
69 lines (62 loc) · 893 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int a, b, c;
};
int pre[100001], n, m, ans;
node d[200001];
bool cmp(node x, node y)//自定义结构体排序函数
{
return x.c < y.c;
}
void init()//初始化并查集数组
{
for (int i = 1; i <= n; i++)
{
pre[i] = i;
}
}
int find(int x)//并查集查询函数
{
if (x == pre[x])
{
return x;
}
return pre[x] = find(pre[x]);//路径压缩
}
int main()
{
std::ios::sync_with_stdio(false);
cin >> n >> m;
init();
for (int i = 0; i < m; i++)
{
cin >> d[i].a >> d[i].b >> d[i].c;
}
sort(d, d + m, cmp);
for (int i = 0; i < m; i++)
{
int x = find(d[i].a);
int y = find(d[i].b);
if (x != y)
{//设置父元素
if (x > y)
{
pre[x] = y;
}
else
{
pre[y] = x;
}
ans = max(ans, d[i].c);
}
if (1 == find(n))//如果1-n连通,则输出结果
{
break;
}
}
cout << ans << endl;
return 0;
}