ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1022. Genealogical Tree

crash
Posted by viktor 10 Mar 2007 16:16
Why do I get access violation? here's the code(top_sort with pointers)

type pok=^mars;
     mars=record
     redbr:integer;
     sleden:pok;
     end;
     var i,j,k,l,n,m,gl,r,pm,pm1:integer;
     brdeca,z,odg:array[1..100]of integer;
     p1,p2,p3,pom,prv:pok;
     toc:boolean;
     vl,kon,vl1:array[1..200]of integer;
     deca:array[1..150,1..150]of integer;
     t,tc,ozn:array[1..200]of boolean;
procedure stavi(y:integer);
begin
     if prv=nil then
     begin
          new(p1);
          prv:=p1;
          p1^.redbr:=y;
          p1^.sleden:=nil;
          kon[y]:=gl;
          gl:=gl+1;
     end else
     begin
          pom:=prv;
          while pom^.sleden<>nil do pom:=pom^.sleden;
          new(p2);
          pom^.sleden:=p2;
          p2^.sleden:=nil;
          p2^.redbr:=y;
          kon[y]:=gl;
          gl:=gl+1;
     end;
end;
procedure dfs(x:integer);
begin
      if tc[x]=false then
      begin
           tc[x]:=true;
           for j:=1 to brdeca[x] do vl[deca[x,j]]:=vl[deca[x,j]]+1;
           for j:=1 to brdeca[x] do if tc[deca[x,j]]=false then dfs(deca[x,j]);
      end;
end;
procedure vadi;
begin
          r:=prv^.redbr;
          for i:=1 to brdeca[prv^.redbr] do vl[deca[r,i]]:=vl[deca[r,i]]-1;
          pom:=prv;
          prv:=prv^.sleden;
          dispose(pom);
end;
begin
readln(n);
for i:=1 to n do ozn[i]:=false;
for i:=1 to n do vl[i]:=0;
for i:=1 to n do brdeca[i]:=1;
i:=1;
while i<n+1 do
begin
      read(deca[i,brdeca[i]]);
      if deca[i,brdeca[i]]<>0 then brdeca[i]:=brdeca[i]+1
      else
          begin
               brdeca[i]:=brdeca[i]-1;
               i:=i+1;
          end;
end;
gl:=0;
for i:=1 to n do
tc[i]:=false;
for i:=1 to n do dfs(i);
for i:=1 to n do vl1[i]:=vl[i];
toc:=true;
new(prv);
prv^.sleden:=nil;
for i:=1 to n do if vl[i]=0 then stavi(i);
while toc do
begin
     vadi;
     for i:=1 to brdeca[r] do if vl[deca[r,i]]=0 then stavi(deca[r,i]);
     if prv=nil then toc:=false;
end;
for i:=1 to n do z[i]:=i;
for i:=1 to n-1 do
begin
     for j:=i+1 to n do
     begin
          if kon[i]>kon[j] then
          begin
              pm:=z[i];
              pm1:=kon[i];
              z[i]:=z[j];
              kon[i]:=kon[j];
              z[j]:=pm;
              kon[j]:=pm1;
          end;
      end;
end;
for i:=1 to n-1 do write(z[i],' ');
write(z[n]);
end.