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
| // kruskal
const int VN = 1005, VM = 20005;
struct KK{ // init is necessary
int N, M, par[VN];
struct Edge{
int f, t, l;
Edge(int F=0, int T=0, int L=0):f(F), t(T), l(L){}
inline bool operator < (const Edge& a)const{
return l<a.l;
}
}e[VM];
inline void init(int N_){
N = N_, M = 0;
for(int i=0;i<N;i++) par[i] = i;
}
inline void insert(int f, int t, int l){
e[M++] = Edge(f,t,l);
}
inline void get(int M_){
M = M_;
for(int i=0;i<M;i++){
cin >> e[i].f >> e[i].t >> e[i].l;
e[i].f--, e[i].t--;
}
}
inline int build(){
int t1, t2, res=0;
sort(e, e+M);
for(int i=0;i<M;i++){
t1 = _find(e[i].f), t2 = _find(e[i].t);
if(t1 == t2) continue;
// there, get a edge in mst
res = max(res, e[i].l);
(i&1) ? par[t1] = t2 : par[t2] = t1;
}
return res;
}
int _find(int v) {
return ( v == par[v] ? v : par[v] = _find(par[v]));
}
}kk;
void init(){
int N, M;
N = getint(), M = getint();
kk.init(N);
kk.get(M);
}
void sol(){
printf("%d\n", kk.build());
}
int main(){
init();
sol();
} |