Pagini recente » Cod sursa (job #1597967) | Cod sursa (job #1279046) | Cod sursa (job #2630091) | Cod sursa (job #2068420) | Cod sursa (job #677246)
Cod sursa(job #677246)
Program Kruscal;
type tip=record
x,y,t:longint;
end;
var a:array [1..400000] of tip;
l,sol1,sol2:array [1..100000] of longint;
i,j,n,m,ct,k,v,w:longint;
fi,fo:text;
procedure sort(l,r:longint);
var k,i,j:longint;
y:tip;
begin
i:=l; j:=r;
k:=a[(l+r) div 2].t;
repeat
while a[i].t<k do inc(I);
while a[j].t>k do dec(j);
if i<=j then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i); dec(j);
end;
until i>=j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
assign(fi,'apm.in');
assign(fo,'apm.out');
reset(fi); rewrite(fo);
readln(fi,n,m);
for i:=1 to n do l[i]:=i;
for i:=1 to m do readln(fi,a[i].x,a[i].y,a[i].t);
sort(1,m); i:=1;
while k<n-1 do begin
if l[a[i].x]<>l[a[i].y] then begin
inc(k);
ct:=ct+a[i].t;
sol1[k]:=a[i].x; sol2[k]:=a[i].y;
v:=l[a[i].y]; w:=l[a[i].x];
for j:=1 to n do
if l[j]=v then l[j]:=w;
end;
inc(i);
end;
writeln(fo,ct); writeln(fo,n-1);
for i:=1 to n-1 do writeln(fo,sol2[i],' ',sol1[i]);
close(fo);
end.