Problem p01358 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int n, v, u, m, U[1000001], V[1000001], M[100000], sumu = 0, sumv = 0, x;
int HU[501] = {}, WU[501] = {}, unaname1 = 0, unaname2 = 0;
int HV[501] = {}, WV[501] = {}, vnaname1 = 0, vnaname2 = 0;

int main() {

  for (int i = 0; i < 1000001; i++) {
    U[i] = -1;
    V[i] = -1;
  }
  cin >> n >> u >> v >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      U[x] = i * n + j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      V[x] = i * n + j;
    }
  }

  for (int i = 0; i < m; i++)
    cin >> M[i];

  for (int k = 0; k < m; k++) {
    if (U[M[k]] != -1) {
      int a = U[M[k]] / n, b = U[M[k]] % n;
      HU[a]++;
      WU[b]++;
      if (HU[a] == n)
        sumu++;
      if (WU[b] == n)
        sumu++;
      if (a == b) {
        unaname1++;
        if (unaname1 == n)
          sumu++;
      }
      if (a + b == n - 1) {
        unaname2++;
        if (unaname2 == n)
          sumu++;
      }
    }

    if (V[M[k]] != -1) {
      int a = V[M[k]] / n, b = V[M[k]] % n;
      HV[a]++;
      WV[b]++;
      if (HV[a] == n)
        sumv++;
      if (WV[b] == n)
        sumv++;
      if (a == b) {
        vnaname1++;
        if (vnaname1 == n)
          sumv++;
      }
      if (a + b == n - 1) {
        vnaname2++;
        if (vnaname2 == n)
          sumv++;
      }
    }

    if (n == 1) {
      sumu = min(sumu, 1);
      sumv = min(sumv, 1);
    }
    if (sumu >= u && sumv >= v) {
      cout << "DRAW" << endl;
      break;
    }
    if (sumu >= u) {
      cout << "USAGI" << endl;
      break;
    }
    if (sumv >= v) {
      cout << "NEKO" << endl;
      break;
    }
    if (k == m - 1)
      cout << "DRAW" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, v, u, m, U[500][500], V[500][500], M[1000000];
bool UU[500][500], VV[500][500];

bool checkU(int d) {
  int ii = -1, jj = -1, a, b, count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (U[i][j] == d) {
        UU[i][j] = true;
        ii = i;
        jj = j;
        break;
      }
    }
    if (ii != -1 && jj != -1)
      break;
  }
  if (ii == -1 && jj == -1)
    return false;
  if (n == 1 && u != 1)
    return false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!UU[i][j])
        break;
      if (j == n - 1)
        count++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!UU[j][i])
        break;
      if (j == n - 1)
        count++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!UU[i][i])
      break;
    if (i == n - 1)
      count++;
  }
  for (int i = 0; i < n; i++) {
    if (!UU[i][n - i - 1])
      break;
    if (i == n - 1)
      count++;
  }
  if (count >= u)
    return true;
  return false;
}

bool checkV(int d) {
  int ii = -1, jj = -1, a, b, count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (V[i][j] == d) {
        VV[i][j] = true;
        ii = i;
        jj = j;
      }
    }
  }
  if (ii == -1 && jj == -1)
    return false;

  if (n == 1 && v != 1)
    return false;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!VV[i][j])
        break;
      if (j == n - 1)
        count++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!VV[j][i])
        break;
      if (j == n - 1)
        count++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!VV[i][i])
      break;
    if (i == n - 1)
      count++;
  }
  for (int i = 0; i < n; i++) {
    if (!VV[i][n - i - 1])
      break;
    if (i == n - 1)
      count++;
  }
  if (count >= v)
    return true;
  return false;
}

int main() {
  cin >> n >> u >> v >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      UU[i][j] = false;
      VV[i][j] = false;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> U[i][j];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> V[i][j];
  }
  for (int i = 0; i < m; i++)
    cin >> M[i];

  for (int k = 0; k < m; k++) {
    bool a = checkU(M[k]);
    bool b = checkV(M[k]);

    if (a && b) {
      cout << "DRAW" << endl;
      break;
    }
    if (a) {
      cout << "USAGI" << endl;
      break;
    }
    if (b) {
      cout << "NEKO" << endl;
      break;
    }
    if (k == m - 1)
      cout << "DRAW" << endl;
  }
  return 0;
}