Cod sursa(job #58335)

Utilizator gurneySachelarie Bogdan gurney Data 5 mai 2007 14:44:07
Problema Atac Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 4.91 kb
program atac;
  const
    fin='atac.in';
    fout='atac.out';
    nmax=35000;
type
  edges=^edge;
  edge=record
    x:longint;cost:longint;urm:edges;
    end;
  pv=^v;
  v=array[1..4*nmax] of longint;
  pvect=^vect;
  vect=array[1..nmax] of longint;
var
  tata:array[0..nmax,0..16] of integer;
  minim:array[0..nmax,0..16] of longint;
  fiu:array[1..nmax] of edges;
  st:pvect;
  ist,dim,log:longint;
  arb:pv;
  viz:array[1..nmax] of boolean;
  e:array[1..2*nmax-1] of longint;
  h:array[1..nmax] of longint;
  min,a,b,c,d,max,n1,n2,p,rad,i,j,k,n,m,x,y,l,r,z:longint;
  aux,q:edges;

procedure query(index,st,dr:longint);
  var
    mid:longint;
  begin
    if (l<=st)and(dr<=r) then
      begin
        if h[arb^[index]]<min then
          begin
            min:=h[arb^[index]];
            rad:=arb^[index];
          end;
      end
    else
      begin
        mid:=(st+dr)shr 1;
        if l<=mid then
          query(index shl 1,st,mid);
        if r>mid then
          query(index shl 1 or 1,mid+1,dr);
      end;
  end;

procedure build_tree(index,st,dr:longint);
  var
    mid:longint;
  begin
    if st=dr then
      begin
        arb^[index]:=e[st];
      end
    else
      begin
        mid:=(st+dr)shr 1;
        build_tree(index shl 1,st,mid);
        build_tree(index shl 1 or 1,mid+1,dr);
        if h[arb^[index shl 1]]<h[arb^[index shl 1 or 1]] then
          arb^[index]:=arb^[index shl 1]
        else
          arb^[index]:=arb^[index shl 1 or 1];
      end;
  end;

procedure insert(x,y,cost:longint);
  var
    aux:edges;
  begin
    new(aux);aux^.x:=y;
    aux^.cost:=cost;
    aux^.urm:=fiu[x]^.urm;
    fiu[x]^.urm:=aux;
  end;

function zebra(x,y:integer):longint;
  var
    a,b,cnt,aux:longint;
  begin
    l:=st^[x];r:=st^[y];
    if l>r then
      begin
        aux:=l;l:=r;r:=aux;
      end;
    min:=maxlongint shr 1;
    query(1,1,dim);
    min:=maxlongint shr 1;
    a:=h[x]-h[rad];
    cnt:=1 shl log;
    aux:=log;
    while (cnt>=1)and(x<>0) do
      begin
        if a>=cnt then
          begin
            if minim[x,aux]<min then
              begin
                min:=minim[x,aux];
              end;
            x:=tata[x,aux];
            dec(a,cnt);
          end;
        cnt:=cnt shr 1;
        dec(aux);
      end;
    a:=h[y]-h[rad];
    cnt:=1 shl log;
    aux:=log;
    while (cnt>=1)and(y<>0) do
      begin
        if a>=cnt then
          begin
            if minim[y,aux]<min then
              begin
                min:=minim[y,aux];
              end;
            y:=tata[y,aux];
            dec(a,cnt);
          end;
        cnt:=cnt shr 1;
        dec(aux);
      end;
    zebra:=min;
  end;

function minimum(x,y:longint):longint;
  begin
    if x<y then
      minimum:=x
    else minimum:=y;
  end;

begin
  assign(input,fin);
    reset(input);
    readln(n,m,p);
    new(arb);
    for i:=1 to n do
      begin
        new(fiu[i]);fiu[i]^.urm:=nil;
      end;
    for i:=2 to n do
      begin
        readln(x,y);
        insert(i,x,y);
        insert(x,i,y);
      end;
    new(st);
    rad:=1;
    log:=0;
    while 1 shl (log+1)<=n do
      inc(log);
    h[rad]:=0;dim:=1;e[1]:=rad;
    for i:=1 to n do
      fiu[i]:=fiu[i]^.urm;
    ist:=1;st^[ist]:=rad;
    while ist>0 do
      begin
        viz[st^[ist]]:=true;
        if fiu[st^[ist]]=nil then
          x:=-1
        else
          while (viz[fiu[st^[ist]]^.x]) do
            begin
              q:=fiu[st^[ist]];
              fiu[st^[ist]]:=fiu[st^[ist]]^.urm;
              dispose(q);
              if fiu[st^[ist]]=nil then
                begin
                  x:=-1;
                  break;
                end;
            end;
        if fiu[st^[ist]]<>nil then
          begin
            x:=fiu[st^[ist]]^.x;
            minim[x,0]:=fiu[st^[ist]]^.cost;
          end;
        if x<>-1 then
          begin
            tata[x,0]:=st^[ist];
            inc(ist);
            st^[ist]:=x;
            inc(dim);e[dim]:=x;h[st^[ist]]:=h[st^[ist-1]]+1;
          end
        else
          begin
            dec(ist);
            if ist>0 then
              begin
                inc(dim);e[dim]:=st^[ist];
              end;
          end;
      end;
    for i:=1 to dim do
      st^[e[i]]:=i;
    minim[1,0]:=maxlongint shr 1;
    build_tree(1,1,dim);
    for k:=1 to log do
      for i:=1 to n do
        begin
          tata[i,k]:=tata[tata[i,k-1],k-1];
          minim[i,k]:=minimum(minim[tata[i,k-1],k-1],minim[i,k-1]);
        end;
    readln(x,y,a,b,c,d);
  close(input);
  assign(output,fout);
    rewrite(output);
    z:=zebra(x,y);
    for i:=1 to m-1 do
      begin
        if (m-p+1)<=i then
          writeln(z);
        x:=(x+y) mod n +1;
        y:=(y+z) mod n +1;
        z:=zebra(x,y);
      end;
    if p>=1 then
      writeln(z);
  close(output);
end.