- 繁忙的都市
- @ 2014-04-05 10:34:52
样例过了,数据只过了一个点
type node=record
       left,right,v:longint;
       end;
var m,n:longint;
     g:array[1..10000] of node;
     father:array[1..10000] of integer;
     max:longint;
procedure init;
var i:longint;
begin
     read(n,m);
     for i:=1 to m do father[i]:=i;
     for i:=1 to m do 
        read(g[i].left,g[i].right,g[i].v);
end;        
procedure sort(l,r: longint);
      var
         i,j,x: longint;
         y:node;
      begin
         i:=l;
         j:=r;
         x:=g[random(r-l+1)+l].v;
         repeat
           while g[i].v<x do
            inc(i);
           while x<g[j].v do
            dec(j);
           if not(i>j) then
             begin
                y:=g[i];
                g[i]:=g[j];
                g[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;   
function getfather(v:longint):longint;
begin
     if father[v]=v then exit(v)
         else father[v]:=getfather(father[v]);
     exit(father[v]);
end;
function union(x,y:longint):boolean;
var fx,fy:longint;
begin
     fx:=getfather(father[x]);
     fy:=getfather(father[y]);
     if fx=fy then exit(true)
            else union:=false;
     father[fx]:=fy;
end;
procedure kruscal;
var cnt,i,ans:longint;
begin
     cnt:=0;ans:=0;
     for i:=1 to n do
         if getfather(g[i].left) <> getfather(g[i].right) then
         begin
             union(g[i].left,g[i].right);
             if max<g[i].v then max:=g[i].v;
             inc(cnt);
             if cnt=n-1 then break;
         end;
end;         
begin
     max:=0;
     randomize;
     init;
     sort(1,m);
     kruscal;
     write(n-1,' ',max);
end.     
4 条评论
- 
  !TNT! LV 8 @ 2014-08-10 16:40:00沙发 
- 
  @ 2014-08-10 16:39:37!!_!! 
- 
  @ 2014-04-12 19:27:46应该用for 
- 
  @ 2014-04-11 23:20:51kruscal的部分错了。它不能保证(n-1)条边都加入最小生成树的要求。需要把for改成while 
- 1