What is wrog? (recursive DP solution)
Hi! I tried to solve using recursive dp (memoization). I got WA, so
could you tell me what is wrong or give me some test cases? Thanks!
type team=record
memb:array[1..3] of string;
end;
var t:array[1..20] of team;
a:array[1..20,1..20] of integer; {a[i,j]=1 if teams i,j share at
least a member, 0 otherwise}
incl,decl:array[1..20] of integer; {incl[i], decl[i] - maximum
teams that can be selected from i's conex component if we include i
respectively if we don't}
viz:array[1..20] of 0..1; {viz[i]=1 if we went through team i, 0
otherwise}
n,i,j,k,l:integer; s:string;
procedure solve(x:integer);
var i:integer;
begin
viz[x]:=1;
decl[x]:=0;
incl[x]:=1;
for i:=1 to n do
if (viz[i]=0) and (a[x,i]=1) then begin
solve(i);
incl[x]:=incl[x]+decl[i];
if incl[i]>decl[i] then decl[x]:=decl[x]+incl[i]
else decl[x]:=decl[x]+decl[i]; {if team x
is not invited, we can invite team i, but we only want to do so if
incl[i]>decl[i]}
end;
end;
begin
{assign(input,'input.txt');
reset(input);}
fillchar(a,sizeof(a),0);
readln(n);
for i:=1 to n do begin
readln(s);
t[i].memb[1]:=copy(s,1,pos(' ',s)-1);
delete(s,1,pos(' ',s));
while s[1]=' ' do delete(s,1,1);
t[i].memb[2]:=copy(s,1,pos(' ',s)-1);
delete(s,1,pos(' ',s));
while s[1]=' ' do delete(s,1,1);
while not (s[length(s)] in ['a'..'z']) do delete(s,length(s),1);
t[i].memb[3]:=s;
for j:=1 to i-1 do
for l:=1 to 3 do
if (t[i].memb[1]=t[j].memb[l]) or
(t[i].memb[2]=t[j].memb[l]) or
(t[i].memb[3]=t[j].memb[l]) then begin
a[i,j]:=1; a[j,i]:=1;
break;
end;
end;
fillchar(viz,sizeof(viz),0);
k:=0;
for i:=1 to n do
if viz[i]=0 then begin {solve for each component of the graph
and add the maximum team selected from it}
solve(i);
if incl[i]>decl[i] then k:=k+incl[i]
else k:=k+decl[i];
end;
writeln(k);
end.