Cod sursa(job #541673)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 25 februarie 2011 13:11:30
Problema Lazy Scor 10
Compilator fpc Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.91 kb
type muchie=record
u,v,cost,c2,poz:longint;
end;
graf=array[1..200000]of muchie;
var g:graf;
x,c:array[1..100000]of longint;
c2,aux2,i1,i2,i3,j,i,n,m,k,a,b:longint;
aux:muchie;
q,ok:boolean;
f,h:text;
x1,x2:int64;
procedure divid(st,dr:longint);
 var p,i,j:longint;
 begin
 i:=st;
 j:=dr;
 p:=g[(st+dr)shr 1].cost;
 while (i<=j) do
    begin
      while (g[i].cost<p) do i:=i+1;
      while (g[j].cost>p) do j:=j-1;
     if i<=j then begin
              aux:=g[i];
              g[i]:=g[j];
              g[j]:=aux;
              i:=i+1;
              j:=j-1;
              end;
    end;
    if st<j then divid(st,j);
    if dr>i then divid(i,dr);
end;

procedure divid2(st,dr:longint);
 var p,i,j:longint;
 begin
 i:=st;
 j:=dr;
 p:=x[(st+dr)shr 1];
 while (i<=j) do
    begin
      while (x[i]<p) do i:=i+1;
      while (x[j]>p) do j:=j-1;
     if i<=j then begin
              aux2:=x[i];
              x[i]:=x[j];
              x[j]:=aux2;
              i:=i+1;
              j:=j-1;
              end;
    end;
    if st<j then divid2(st,j);
    if dr>i then divid2(i,dr);
end;


begin
assign(f,'lazy.in');
reset(f);
assign(h,'lazy.out');
rewrite(h);
read(f,n,m);
for i:=1 to m do
  begin
  read(f,i1,i2,i3,c2);
  g[i].u:=i1;
  g[i].v:=i2;
  g[i].cost:=i3;
  g[i].c2:=c2;
  g[i].poz:=i;
  end;
divid(1,m);
{
q:=true;
while q do
begin
q:=false;
for i:=1 to m-1 do
if g[i].cost=g[i+1].cost then
begin
x1:=g[i].cost*g[i].c2;
x2:=g[i+1].cost*g[i+1].c2;
if x1<x2 then begin aux:=g[i];g[i]:=g[i+1];g[i+1]:=g[i];q:=true;end;
end;
end;
 }
for i:=1 to i do
c[i]:=i;
i:=1;
k:=0;
while (i<=m)and (k<n-1) do
begin
if c[g[i].u]<>c[g[i].v] then
    begin
    a:=c[g[i].u];
    b:=c[g[i].v];
    for j:=1 to n do
    if c[j]=a then c[j]:=b;
    k:=k+1;
    x[k]:=g[i].poz;
    end;
i:=i+1;
end;
divid2(1,k);
for i:=1 to k do
writeln(h,x[i]);
close(h);
end.