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 1208. Legendary Teams Contest

What is wrog? (recursive DP solution)
Posted by Vlad Ionescu 11 Jul 2003 06:36
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.
I got AC!!!
Posted by Vlad Ionescu 11 Jul 2003 07:52
I finally got AC!
The problem was with tests like this:

3
a b c
c d e
e f a

My first program would output 2, but the answer is obviously 1. I
just had to introduce another if and decrease incl[x] in case there
was a cycle in the graph.
Congratulations! (-)
Posted by Dmitry 'Diman_YES' Kovalioff 12 Jul 2003 18:24