Cod sursa(job #43503)

Utilizator AymdTrimbitas Viorel Stefan Aymd Data 30 martie 2007 11:01:58
Problema Balanta Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.14 kb
var n,m,ip,ic,i,j,s:integer;
    a,c,o,u,d:array[0..1025]of integer;
    p:array[1..1024,0..1025]of integer;
    f,g:text;

begin
assign(f,'balanta.in');
reset(f);
readln(f,n,m);
for i:=1 to m do begin
   read(f,ic);
   ic:=ic*2;
   for j:=1 to ic do read(f,c[j]);
   readln(f,s);
   if s=0 then for j:=1 to ic do a[c[j]]:=1
          else begin
               ip:=ip+1;
               p[ip]:=c;
               p[ip,0]:=ic;
               p[ip,1025]:=s;
               for j:=1 to ic do o[c[j]]:=1;
               end;
end;
assign(g,'balanta.out');
rewrite(g);
if ip=0 then begin
             s:=0;
             for i:=1 to n do begin
                if a[i]=0 then ic:=i
                          else s:=s+1;
             end;
             if s=n-1 then write(g,ic)
                      else write(g,0);
             end
        else begin
             for i:=1 to ip do begin
                if p[i,1025]=1 then begin
                                    for j:=1 to p[i,0] div 2 do d[p[i,j]]:=1;
                                    for j:=j+1 to 2*j do u[p[i,j]]:=1;
                                    end;
                if p[i,1025]=2 then begin
                                    for j:=p[i,0] div 2+1 to p[i,0]do d[p[i,j]]:=1;
                                    for j:=1 to p[i,0] div 2 do u[p[i,j]]:=1;
                                    end;
             end;
             for i:=1 to n do if a[i]=1 then begin
                                             u[i]:=1;
                                             d[i]:=1;
                                             end;
             s:=0;
             for i:=1 to n do
                if u[i]=1 then s:=s+1
                          else ic:=i;
             if n-s=1 then write(g,ic)
                      else begin
                           s:=0;
                           for i:=1 to n do
                              if d[i]=1 then s:=s+1
                                        else ic:=i;
                           if n-s=1 then write(g,ic)
                                    else write(g,0);
                           end;
             end;
close(g);
end.