ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1208. Соревнование легендарных команд

What is wrog? (recursive DP solution)
Послано Vlad Ionescu 11 июл 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!!!
Послано Vlad Ionescu 11 июл 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! (-)
Послано Dmitry 'Diman_YES' Kovalioff 12 июл 2003 18:24