Cod sursa(job #764886)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 6 iulie 2012 16:58:51
Problema Lazy Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.38 kb
Program lazy;
 type tip=record
         x,y,pos:longint;
         c,p:int64;
         end;
 var a:array [1..200001] of tip;
     n,m,i,c2,k,x,y:longint;
     tata:array [1..200001] of longint;
     fi,fo:text;
procedure sort(l,r:longint);
 var i,j:longint;
     k,k2:int64;
     y:tip;
begin
 i:=l; j:=r; k:=a[(l+r) div 2].c; k2:=a[(l+r) div 2].p;
 repeat
  while (a[i].c<k) or ((a[i].c=k) and (a[i].p>k2)) do inc(i);
   while (a[i].c>k) or ((a[i].c=k) and (a[i].p<k2)) 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 i<r then sort(i,r);
  if j>l then sort(l,j);
end;
function radacina(nod:longint):longint;
 var aux,r:longint;
begin
 aux:=nod; r:=nod;
  while tata[r]<>r do r:=tata[r];
 radacina:=r;
  while tata[nod]<>nod do begin aux:=tata[nod]; tata[nod]:=r; nod:=aux; end;
end;
begin
 assign(fi,'lazy.in');
  assign(fo,'lazy.out');
 reset(fi); rewrite(fo); readln(fi,n,m);
  for i:=1 to m do begin
                   readln(fi,a[i].x,a[i].y,a[i].c,c2);
                   a[i].p:=a[i].c*c2; a[i].pos:=i;
                   end;
  sort(1,n);
   for i:=1 to n do tata[i]:=i;
  i:=1; k:=n-1;
  while k>0 do begin
   x:=radacina(a[i].x); y:=radacina(a[i].y);
    if x<>y then begin dec(k); writeln(fo,a[i].pos); tata[x]:=y; end;
   inc(i);
  end;
 close(fo);
end.