Pagini recente » Atasamentele paginii Clasament concurs_11_12_02_23 | Cod sursa (job #1351421) | Cod sursa (job #1623804) | Cod sursa (job #1726112) | Cod sursa (job #677250)
Cod sursa(job #677250)
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;
b1,b2:array [1..1 shl 17] of char;
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');
settextbuf(fi,b1); settextbuf(fo,b2);
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.