Problem p01358 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, u, v, m, in, ru, cu, rn, cn, countu = 0, countn = 0;
  bool uw = false, nw = false;
  cin >> n >> u >> v >> m;
  vector<int> rowu(n, 0), colu(n, 0), crsu(2, 0), rown(n, 0), coln(n, 0),
      crsn(2, 0);
  vector<pair<int, int>> dicu(1000000, make_pair(-1, -1)),
      dicn(1000000, make_pair(-1, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> in;
      dicu[in - 1] = make_pair(i, j);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> in;
      dicn[in - 1] = make_pair(i, j);
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> in;
    ru = dicu[in - 1].first;
    cu = dicu[in - 1].second;
    if (!(ru == -1 || cu == -1)) {
      rowu[ru]++;
      if (rowu[ru] == n)
        countu++;
      colu[cu]++;
      if (colu[cu] == n && n != 1)
        countu++;
      if (ru == cu) {
        crsu[0]++;
        if (crsu[0] == n && n != 1)
          countu++;
      }
      if (n - 1 == ru + cu) {
        crsu[1]++;
        if (crsu[1] == n && n != 1)
          countu++;
      }
    }
    rn = dicn[in - 1].first;
    cn = dicn[in - 1].second;
    if (!(rn == -1 || cn == -1)) {
      rown[rn]++;
      if (rown[rn] == n)
        countn++;
      coln[cn]++;
      if (coln[cn] == n && n != 1)
        countn++;
      if (rn == cn) {
        crsn[0]++;
        if (crsn[0] == n && n != 1)
          countn++;
      }
      if (n - 1 == rn + cn) {
        crsn[1]++;
        if (crsn[1] == n && n != 1)
          countn++;
      }
    }
    if (countu >= u)
      uw = true;
    if (countn >= v)
      nw = true;
    if (uw && nw) {
      cout << "DRAW" << endl;
      return 0;
    }
    if (uw) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (nw) {
      cout << "NEKO" << endl;
      return 0;
    }
  }
  cout << "DRAW" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, u, v, m, in, ru, cu, rn, cn, countu = 0, countn = 0;
  bool uw = false, nw = false;
  cin >> n >> u >> v >> m;
  vector<int> rowu(n, 0), colu(n, 0), crsu(2, 0), rown(n, 0), coln(n, 0),
      crsn(2, 0);
  vector<pair<int, int>> dicu(100000, make_pair(-1, -1)),
      dicn(100000, make_pair(-1, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> in;
      dicu[in - 1] = make_pair(i, j);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> in;
      dicn[in - 1] = make_pair(i, j);
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> in;
    ru = dicu[in - 1].first;
    cu = dicu[in - 1].second;
    if (!(ru == -1 || cu == -1)) {
      rowu[ru]++;
      if (rowu[ru] == n)
        countu++;
      colu[cu]++;
      if (colu[cu] == n && n != 1)
        countu++;
      if (ru == cu) {
        crsu[0]++;
        if (crsu[0] == n && n != 1)
          countu++;
      }
      if (n - 1 == ru + cu) {
        crsu[1]++;
        if (crsu[1] == n && n != 1)
          countu++;
      }
    }
    rn = dicn[in - 1].first;
    cn = dicn[in - 1].second;
    if (!(rn == -1 || cn == -1)) {
      rown[rn]++;
      if (rown[rn] == n)
        countn++;
      coln[cn]++;
      if (coln[cn] == n && n != 1)
        countn++;
      if (rn == cn) {
        crsn[0]++;
        if (crsn[0] == n && n != 1)
          countn++;
      }
      if (n - 1 == rn + cn) {
        crsn[1]++;
        if (crsn[1] == n && n != 1)
          countn++;
      }
    }
    if (countu >= u)
      uw = true;
    if (countn >= v)
      nw = true;
    if (uw && nw) {
      cout << "DRAW" << endl;
      return 0;
    }
    if (uw) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (nw) {
      cout << "NEKO" << endl;
      return 0;
    }
  }
  cout << "DRAW" << endl;
  return 0;
}