Why I get WA? Pelase, help me!!!!!!! (+)
My code:
Program t1109;
Const MaxN=1000;
MaxK=500000;
Var A,B :array[1..MaxN]of integer;
AU,BU :array[1..MaxN]of integer;
Ver :array[1..MaxK,1..2]of integer;
M,N,K,i :integer;
Mi,Ni :integer;
Ans :integer;
ex :boolean;
Function Max(A,B:integer):integer;
begin
If A>B then Max:=A else Max:=B;
end;
begin
FillChar(A,SizeOf(A),0);
FillChar(B,SizeOf(B),0);
FillChar(AU,SizeOf(AU),0);
FillChar(BU,SizeOf(BU),0);
Read(N,M,K);
for i:=1 to K do begin
read(ver[i,1],ver[i,2]);
inc(a[ver[i,1]]);
inc(b[ver[i,2]]);
end;
Ans:=0;
Ni:=N;
Mi:=M;
repeat
ex:=true;
for i:=1 to K do
if a[ver[i,1]]=1 then
if AU[ver[i,1]]=0 then begin
Ans:=Ans+1;
if BU[ver[i,2]]=0 then dec(Mi);
BU[ver[i,2]]:=1;
AU[ver[i,1]]:=1;
dec(a[ver[i,1]]);
dec(b[ver[i,2]]);
dec(Ni);
ex:=false;
end;
for i:=1 to K do
if AU[ver[i,1]]=1 then
if BU[ver[i,2]]=0 then begin
ex:=false;
if B[ver[i,2]]=1 then begin
Ans:=Ans+1;
BU[ver[i,2]]:=1;
dec(Mi);
end;
dec(a[ver[i,1]]);
dec(b[ver[i,2]]);
end;
for i:=1 to K do
if b[ver[i,2]]=1 then
if BU[ver[i,2]]=0 then begin
Ans:=Ans+1;
if AU[ver[i,1]]=0 then dec(Ni);
AU[ver[i,1]]:=1;
BU[ver[i,2]]:=1;
dec(a[ver[i,1]]);
dec(b[ver[i,2]]);
dec(Mi);
ex:=false;
end;
for i:=1 to K do
if BU[ver[i,2]]=1 then
if AU[ver[i,1]]=0 then begin
ex:=false;
if A[ver[i,1]]=1 then begin
Ans:=Ans+1;
AU[ver[i,1]]:=1;
dec(Ni);
end;
dec(a[ver[i,1]]);
dec(b[ver[i,2]]);
end;
until ex;
Ans:=Ans+Max(Mi,Ni);
Writeln(Ans);
end.