Cant find the problem... I'm always getting WA...

Code: Select all

```
const maxn = 100;
var a : array [0..maxn,0..maxn] of integer;
q,l : array [0..maxn*maxn] of integer;
c,n,i,x,y,st,max,maxi,t,j,yk,yp,cur : integer;
w : array [0..maxn+1,0..maxn+1] of boolean;
h : boolean;
begin
{ assign(input,'input.txt'); reset(input);
assign(output,'output.txt'); rewrite(output);
}
n:=1;
c:=0;
while n<>0 do
begin
readln(n);
if n>0 then
begin
{}
readln(st);
fillchar(a,sizeof(a),0);
x:=1; y:=1;
while not((x=0)and(y=0)) do
begin
readln(x,y);
if not((x=0)and(y=0)) then
begin
inc(a[x,0]);
a[x,a[x,0]]:=y;
end;
end;
{}
max:=0; maxi:=st;
yk:=1; yp:=1; q[yk]:=st; l[yk]:=0;
while yk<=yp do
begin
cur:=q[yk];
for i:=1 to a[cur,0] do
begin
if w[a[cur,i],l[yk]+1]=false then
begin
w[a[cur,i],l[yk]+1]:=true;
inc(yp);
q[yp]:=a[cur,i];
l[yp]:=l[yk]+1;
end;
end;
if (l[yk]>max)or((l[yk]=max)and(q[yk]<maxi)) then
begin
max:=l[yk];
maxi:=q[yk];
end;
inc(yk);
end;
{}
inc(c);
writeln('Case ',c,': The longest path from ',st,' has length ',max,', finishing at ',maxi,'.');
writeln;
{}
end;
end;
{ close(input); close(output);}
end.
```