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

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  map<int, pair<int, int>> U, N;
  int t;
  if (n == 1) {
    if (u > 1) {
      u += 5;
    }
    if (v > 1) {
      v += 5;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> t;
      U[t] = make_pair(i + 1, j + 1);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> t;
      N[t] = make_pair(i + 1, j + 1);
    }
  }

  int T[m];
  vector<vector<int>> U_(2, vector<int>(n + 1, n)),
      N_(2, vector<int>(n + 1, n));
  for (int i = 0; i < m; ++i)
    cin >> T[i];
  for (int i = 0; i < m; ++i) {
    int k = T[i], s = U[k].first, t = U[k].second;
    if (s && t) {
      --U_[0][s];
      --U_[1][t];
      if (s == t)
        --U_[0][0];
      if (s + t == n + 1)
        --U_[1][0];
      if (!U_[0][s]) {
        --u;
        --U_[0][s];
      }
      if (!U_[1][t]) {
        --u;
        --U_[1][t];
      }
      if (!U_[0][0]) {
        --u;
        --U_[0][0];
      }
      if (!U_[1][0]) {
        --u;
        --U_[1][0];
      }
    }
    s = N[k].first, t = N[k].second;
    if (s && t) {
      --N_[0][s];
      --N_[1][t];
      if (s == t)
        --N_[0][0];
      if (s + t == n + 1)
        --N_[1][0];
      if (!N_[0][s]) {
        --v;
        --N_[0][s];
      }
      if (!N_[1][t]) {
        --v;
        --N_[1][t];
      }
      if (!N_[0][0]) {
        --v;
        --N_[0][0];
      }
      if (!N_[1][0]) {
        --v;
        --N_[1][0];
      }
    }
    if (u <= 0 && v <= 0) {
      cout << "DRAW" << endl;
      return 0;
    } else if (u <= 0) {
      cout << "USAGI" << endl;
      return 0;
    } else if (v <= 0) {
      cout << "NEKO" << endl;
      return 0;
    }
  }
  cout << "DRAW" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int U[500][500];
int N[500][500];
int n, u, v, m;

string judge() {
  int a = 0, b = 0;
  bool f, g;
  for (int i = 0; i < n; ++i) {
    f = true, g = true;
    for (int j = 0; j < n; ++j) {
      if (U[i][j])
        f = false;
      if (U[j][i])
        g = false;
    }
    if (f)
      ++a;
    if (g)
      ++a;
  }
  f = true;
  g = true;
  for (int i = 0; i < n; ++i) {
    if (U[i][i])
      f = false;
    if (U[n - 1 - i][i])
      g = false;
  }
  if (f)
    ++a;
  if (n > 1 && g)
    ++a;

  for (int i = 0; i < n; ++i) {
    f = true, g = true;
    for (int j = 0; j < n; ++j) {
      if (N[i][j])
        f = false;
      if (N[j][i])
        g = false;
    }
    if (f)
      ++b;
    if (g)
      ++b;
  }
  f = true;
  g = true;
  for (int i = 0; i < n; ++i) {
    if (N[i][i])
      f = false;
    if (N[n - 1 - i][i])
      g = false;
  }
  if (f)
    ++b;
  if (n > 1 && g)
    ++b;

  if (a >= u && b >= v)
    return "DRAW";
  if (a >= u)
    return "USAGI";
  if (b >= v)
    return "NEKO";
  return "";
}

int main() {

  cin >> n >> u >> v >> m;
  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 >> N[i][j];

  int T[m];
  for (int i = 0; i < m; ++i)
    cin >> T[i];
  for (int i = 0; i < m; ++i) {
    int t = T[i];
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (U[j][k] == t)
          U[j][k] = 0;
        if (N[j][k] == t)
          N[j][k] = 0;
      }
    }
    if (n == 1) {
      if (((!U[0][0]) && u < 2) && ((!N[0][0]) && v < 2)) {
        cout << "DRAW" << endl;
        return 0;
      }
      if ((!U[0][0]) && u < 2) {
        cout << "USAGI" << endl;
        return 0;
      }
      if ((!N[0][0]) && v < 2) {
        cout << "NEKO" << endl;
        return 0;
      }
    } else {
      string ans = judge();
      if (ans != "") {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << "DRAW" << endl;
  return 0;
}