1 var //解法一——枚举
2 i,j,k,l,m,n:longint;
3 c:array[0..500] of longint;
4 a:array[0..30000,1..3] of longint;
5 PROCEdure swap(var x,y:longint);inline;
6 var z:longint;
7 begin
8 z:=x;x:=y;y:=z;
9 end;
10 procedure sort(l,r:longint);inline;
11 var i,j,x,y:longint;
12 begin
13 i:=l;j:=r;
14 x:=a[(l+r) div 2,3];
15 repeat
16 while a[i,3]<x do inc(i);
17 while a[j,3]>x do dec(j);
18 if i<=j then
19 begin
20 swap(a[i,1],a[j,1]);
21 swap(a[i,2],a[j,2]);
22 swap(a[i,3],a[j,3]);
23 inc(i);dec(j);
24 end;
25 until i>j;
26 if l<j then sort(l,j);
27 if i<r then sort(i,r);
28 end;
29 function getfat(x:longint):longint;inline;
30 begin
31 if x<>c[x] then c[x]:=getfat(c[x]);
32 getfat:=c[x];
33 end;
34 function tog(x,y:longint):boolean;inline;
35 begin
36 exit(getfat(x)=getfat(y));
37 end;
38 procedure merge(x,y:longint);inline;
39 begin
40 c[getfat(x)]:=getfat(y);
41 end;
42 begin
43 readln(n,m);
44 for i:=1 to m do
45 begin
46 readln(a[i,1],a[i,2],a[i,3]);
47 end;
48 sort(1,m);
49 for i:=n-1 to m do
50 begin
51 for j:=1 to n do c[j]:=j;
52 merge(a[i,1],a[i,2]);
53 l:=n-1;
54 for j:=1 to i-1 do
55 begin
56 if tog(a[j,1],a[j,2])=false then
57 begin
58 dec(l);
59 merge(a[j,1],a[j,2]);
60 if l=1 then break;
61 end;
62 end;
63 if l=1 then
64 begin
65 writeln(n-1,‘ ‘,a[i,3]);
66 halt;
67 end;
68 end;
69 end.
1 var //解法二——二分
2 i,j,k,l,m,n,r:longint;
3 c:array[0..500] of longint;
4 a:array[0..30000,1..3] of longint;
5 PROCEdure swap(var x,y:longint);inline;
6 var z:longint;
7 begin
8 z:=x;x:=y;y:=z;
9 end;
10 procedure sort(l,r:longint);inline;
11 var i,j,x,y:longint;
12 begin
13 i:=l;j:=r;
14 x:=a[(l+r) div 2,3];
15 repeat
16 while a[i,3]<x do inc(i);
17 while a[j,3]>x do dec(j);
18 if i<=j then
19 begin
20 swap(a[i,1],a[j,1]);
21 swap(a[i,2],a[j,2]);
22 swap(a[i,3],a[j,3]);
23 inc(i);dec(j);
24 end;
25 until i>j;
26 if l<j then sort(l,j);
27 if i<r then sort(i,r);
28 end;
29 function getfat(x:longint):longint;inline;
30 begin
31 if x<>c[x] then c[x]:=getfat(c[x]);
32 getfat:=c[x];
33 end;
34 function tog(x,y:longint):boolean;inline;
35 begin
36 exit(getfat(x)=getfat(y));
37 end;
38 procedure merge(x,y:longint);inline;
39 begin
40 c[getfat(x)]:=getfat(y);
41 end;
42 function check(i:longint):boolean;inline;
43 var
44 j,l:longint;
45 begin
46 for j:=1 to n do c[j]:=j;
47 merge(a[i,1],a[i,2]);
48 l:=n-1;
49 for j:=1 to i-1 do
50 begin
51 if tog(a[j,1],a[j,2])=false then
52 begin
53 dec(l);
54 merge(a[j,1],a[j,2]);
55 if l=1 then break;
56 end;
57 end;
58 check:=(l=1);
59 end;
60 begin
61 readln(n,m);
62 for i:=1 to m do
63 begin
64 readln(a[i,1],a[i,2],a[i,3]);
65 end;
66 sort(1,m);
67 l:=n-1;r:=m;
68 while l<r do
69 begin
70 if check((l+r) div 2) then r:=(l+r) div 2 else l:=(l+r) div 2+1;
71 end;
72 writeln(n-1,‘ ‘,a[l,3]);
73 end.
74