Cod sursa(job #32940)

Utilizator vlad_popaVlad Popa vlad_popa Data 18 martie 2007 18:52:36
Problema Balanta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <cstdio>
#include <cassert>
#include <string>

#define FIN "balanta.in"
#define FOUT "balanta.out"
#define NMAX 1025

int H[NMAX], L[NMAX], N, M, A[NMAX], B[NMAX];

void del ()
{
  for (int i = 1; i <= N; ++ i)
    if ((A[i] && H[i]) || (B[i] && H[i]))
     {
       H[i] = 0;
       -- H[0];
     }
  for (int i = 1; i <= N; ++ i)
    if ((A[i] && L[i]) || (B[i] && L[i]))
     {
       L[i] = 0;
       -- L[0];
     }
}

void del1 ()
{
  for (int i = 1; i <= N; ++ i)
    if (!A[i] && H[i])
     {
       H[i] = 0;
       -- H[0];
     }

  for (int i = 1; i <= N; ++ i)
    if (!B[i] && L[i])
     {
       L[i] = 0;
       -- L[0];
     }   
}

void del2 ()
{
  for (int i = 1; i <= N; ++ i)
    if (!B[i] && H[i])
     {
       H[i] = 0;
       -- H[0];
     }

  for (int i = 1; i <= N; ++ i)
    if (!A[i] && L[i])
     {
       L[i] = 0;
       -- L[0];
     }   
}

void read ()
{
  int nr, op, x;
  freopen (FIN, "rt", stdin);
  freopen (FOUT, "wt", stdout);

  scanf ("%d%d", &N, &M);

  for (int i = 1; i <= N; ++ i)
    H[i] = L[i] = i;

  H[0] = L[0] = N;

  while (M--)
   {
     memset (A, 0, sizeof (A));
     memset (B, 0, sizeof (B));
     
     scanf ("%d", &nr);

     for (int i = 1; i <= nr; ++ i)
      {
        scanf ("%d", &x);
        A[x] = x;
      }
     for (int i = 1; i <= nr; ++ i)
      {
        scanf ("%d", &x);
        B[x] = x;
      }

     scanf ("%d", &op);

     if (op == 0)
       del ();
     else if (op == 1)
       del1 ();
     else
       del2 ();
   }

  int sol;
   
  if (H[0] + L[0] != 1)
    printf ("0\n");
  else
   {
     if (H[0] == 1)
       for (int i = 1; i <= N; ++ i)
         if (H[i])
           sol = i;
     if (L[0] == 1)
       for (int i = 1; i <= N; ++ i)
         if (L[i])
           sol = i;

     printf ("%d\n", sol);
   }
}

int
 main ()
{
  read ();
  return 0;
}