/ Vijos / 讨论 / 问答 /

请各位大神来帮我看看我这个Kruscal是哪里错了

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.

2 条评论

  • @ 2014-10-29 18:37:25

    勘定是程序卡了,关了再试试,或者检查一下标点,不谢

  • @ 2014-04-12 19:28:38

    应该用for

  • 1