1 /**************************************************************
2 Problem: 1083
3 User: shadowland
4 Language: C++
5 Result: Accepted
6 Time:32 ms
7 Memory:2856 kb
8 ****************************************************************/
9
10 #include "bits/stdc++.h"
11
12 using namespace std ;
13 struct MST { int x , y , val ; } ;
14 const int maxN = 100100 ;
15 const int INF = 2147483647 ;
16 typedef long long QAQ ;
17
18 MST MST_e[ maxN ] ;
19 int father[ maxN ] ;
20
21 void Init_Set ( const int n ) { for ( int i=1 ; i<=n ; ++i ) father[ i ] = i ; }
22 inline bool cmp ( MST a , MST b ) { return a.val < b.val ;}
23 inline int gmax ( int x , int y ) { return x > y ? x : y ; }
24 int getfa ( const int x ) { father[ x ] == x ? x : father[ x ] = getfa ( father[ x ] ) ;}
25 inline void Union_Set ( int x , int y ) { father[ x ] = y ; }
26
27 int MST ( const int N , const int M ) {
28 int _cnt = 0 , _max = -INF ;
29 Init_Set ( N ) ;
30 sort ( MST_e + 1 , MST_e + M + 1 , cmp ) ;
31 for ( int i=1 ; i<=M ; ++i ) {
32 int px = getfa ( MST_e[ i ].x ) ;
33 int py = getfa ( MST_e[ i ].y ) ;
34 if ( px != py ) {
35 ++ _cnt ;
36 Union_Set ( px , py ) ;
37 _max = gmax ( _max , MST_e[ i ].val ) ;
38 }
39 if ( _cnt == N - 1 ) break ;
40 }
41 cout << _cnt << ‘ ‘ ;
42 return _max ;
43 }
44 int main ( ) {
45 int N , M ;
46 scanf ( "%d %d" , &N , &M ) ;
47 for ( int i=1 ; i<=M ; ++i ) {
48 scanf ( "%d %d %d" , &MST_e[ i ].x , &MST_e[ i ].y , &MST_e[ i ].val ) ;
49 }
50 int Ans = MST ( N , M ) ;
51 cout << Ans << endl ;
52 return 0 ;
53 }