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 1109. Conference

Why do I get MLE?(1357K)
Posted by Maigo Akisame (maigoakisame@yahoo.com.cn) 29 Jun 2004 04:52
program ural1109;
const
  max=1000;
var
  g:array[1..max,1..max]of -1..1;
  s,t,cx,cy:array[1..max]of boolean;
  m,n,x,y:integer;
  k,i:longint;
function path(u:integer):boolean;
  var
    i,j:integer;
  begin
    path:=false;
    for j:=1 to n do
      if (g[u,j]=1) and not t[j] then begin
        t[j]:=true;
        if not cy[j] then begin
          g[u,j]:=-1;
          cy[j]:=true;
          path:=true;
          exit;
        end
        else begin
          i:=1;
          while (g[i,j]>=0) and not s[i] and (i<=m) do inc(i);
          if i<=m then begin
            s[i]:=true;
            if path(i) then begin
              g[u,j]:=-1;
              g[i,j]:=1;
              path:=true;
              exit;
            end;
          end;
        end;
      end;
  end;
begin
  fillchar(g,sizeof(g),0);
  readln(m,n,k);
  for i:=1 to k do begin
    readln(x,y);
    g[x,y]:=1;
  end;

  fillchar(cx,sizeof(cx),0);
  fillchar(cy,sizeof(cy),0);
  i:=0;y:=0;
  repeat
    inc(i);
    if cx[i] then continue;
    fillchar(s,sizeof(s),0);s[i]:=true;
    fillchar(t,sizeof(t),0);
    if path(i) then begin
      cx[i]:=true;
      inc(y);
      i:=0;
    end;
  until i=m;

  writeln(m+n-y);
end.