Cod sursa(job #764915)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 6 iulie 2012 17:57:20
Problema Lazy Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.59 kb
Program lazy;
 type tip=record
         x,y,pos:longint;
         c,c2:int64;
         end;
 var a:array [0..200005] of tip;
     n,m,i,k,x,y,poz:longint;
     b1,b2:array [1..1 shl 17] of char;
     tata,indice:array [0..200005] of longint;
     fi,fo:text;
procedure sort(l,r:longint);
 var i,j,y:longint;
     k,k2:int64;
begin
 i:=l; j:=r; k:=a[indice[(l+r) div 2]].c; k2:=a[indice[(l+r) div 2]].c2;
 repeat
  while (a[indice[i]].c<k) or ((a[indice[i]].c=k) and (a[indice[i]].c2>k2) and (i<=r)) do inc(i);
   while (a[indice[j]].c>k) or ((a[indice[j]].c=k) and (a[indice[j]].c2<k2) and (j>=l)) do dec(j);
  if i<=j then begin
               y:=indice[i]; indice[i]:=indice[j]; indice[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');
 settextbuf(fi,b1); settextbuf(fo,b2);
 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,a[i].c2);
                    a[i].pos:=i; indice[i]:=i;
                   end;
  sort(1,m);
   for i:=1 to n do tata[i]:=i;
  i:=1; k:=n-1;
  while k>0 do begin
  poz:=indice[i];
   x:=radacina(a[poz].x); y:=radacina(a[poz].y);
    if x<>y then begin dec(k); writeln(fo,a[poz].pos); tata[x]:=y; end;
   inc(i);
  end;
 close(fo);
end.