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

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;

int toInt(string s) {
  stringstream ss;
  ss << s;
  int a;
  ss >> a;
  return a;
}

int main() {
  int p, s;
  bool flag = false;
  while (cin >> p && p) {
    if (flag)
      cout << endl;
    flag = true;
    cin >> s;
    s++, p++;
    vector<vector<int>> v;
    v.resize(p, vector<int>(s));
    //    cin.ignore();

    string str;
    vector<pair<int, int>> pos;
    int total = 0;
    int r_hatena[p];
    int c_hatena[s];
    int r[p];
    int c[s];

    fill(r_hatena, r_hatena + p, 0);
    fill(c_hatena, c_hatena + s, 0);
    fill(r, r + p, 0);
    fill(c, c + s, 0);

    for (int i = 0; i < p; i++) {
      for (int j = 0; j < s; j++) {
        cin >> str;
        if (str == "?") {
          total++;
          v[i][j] = INF;
          pos.push_back(pair<int, int>(j, i));
        } else
          v[i][j] = toInt(str);
      }
    }

    for (int i = 0; i < p; i++) {
      int sum = 0;
      for (int j = 0; j < s; j++) {
        if (v[i][j] == INF)
          c_hatena[j]++;
        else if (j != s - 1)
          sum += v[i][j];
      }
      r[i] = sum;
    }

    for (int i = 0; i < s; i++) {
      int sum = 0;
      for (int j = 0; j < p; j++) {
        if (v[j][i] == INF)
          r_hatena[j]++;
        else if (j != p - 1)
          sum += v[j][i];
      }
      c[i] = sum;
    }

    priority_queue<pair<bool, int>, vector<pair<bool, int>>,
                   greater<pair<bool, int>>>
        que;

    for (int i = 0; i < s; i++) {
      if (c_hatena[i] == 1) {
        que.push(pair<bool, int>(true, i));
      }
    }

    for (int i = 0; i < p; i++) {
      if (r_hatena[i] == 1) {
        que.push(pair<bool, int>(false, i));
      }
    }

    // p tate s yoko
    // 0 is row 1 is column
    while (!que.empty()) {
      pair<bool, int> tmp = que.top();
      que.pop();

      if (tmp.first == 0) {
        if (r_hatena[tmp.second] <= 0)
          continue;

        for (int i = 0; i < s; i++) {
          if (v[tmp.second][i] == INF) {
            v[tmp.second][i] = v[tmp.second][s - 1] - r[tmp.second];
            //	    cout << v[tmp.second][s-1] << endl;
            //	    cout << r[tmp.second] << endl;
            r_hatena[tmp.second]--;
            c_hatena[i]--;
            //	    cout << tmp.second << " " << i << endl;
            total--;
            c[i] += v[tmp.second][i];
            if (c_hatena[i] == 1)
              que.push(pair<bool, int>(1, i));
          }
        }

      } else {
        if (c_hatena[tmp.second] <= 0)
          continue;
        for (int i = 0; i < p; i++) {
          if (v[i][tmp.second] == INF) {
            //	    cout << i << " " << tmp.second << endl;
            v[i][tmp.second] = v[p - 1][tmp.second] - c[tmp.second];
            c_hatena[tmp.second]--;
            r_hatena[i]--;
            //	    cout << i << " " << tmp.second << endl;
            //	    cout << c_hatena[tmp.second] << endl;
            //	    cout << r_hatena[i] << endl;
            r[i] += v[i][tmp.second];
            total--;
            if (r_hatena[i] == 1)
              que.push(pair<bool, int>(0, i));
            //	    cout << v[i][tmp.second] << endl;
          }
        }
      }
    }

    if (total <= 0) {
      for (int i = 0; i < pos.size(); i++) {
        int x = pos[i].first;
        int y = pos[i].second;
        cout << v[y][x] << endl;
      }
    } else
      cout << "NO" << endl;
    //    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;

int toInt(string s) {
  stringstream ss;
  ss << s;
  int a;
  ss >> a;
  return a;
}

int main() {
  int s, p;
  bool flag = false;
  while (cin >> p && p) {
    if (flag)
      cout << endl;
    flag = true;
    cin >> s;
    s++, p++;
    vector<vector<int>> v;
    v.resize(p, vector<int>(s));
    cin.ignore();

    string str;
    vector<pair<int, int>> pos;
    int total = 0;
    int r_hatena[s];
    int c_hatena[p];
    int r[s];
    int c[p];

    fill(r_hatena, r_hatena + p, 0);
    fill(c_hatena, c_hatena + s, 0);
    fill(r, r + p, 0);
    fill(c, c + s, 0);

    for (int i = 0; i < p; i++) {
      for (int j = 0; j < s; j++) {
        cin >> str;
        if (str == "?") {
          total++;
          v[i][j] = INF;
          pos.push_back(pair<int, int>(j, i));
        } else
          v[i][j] = toInt(str);
      }
    }

    for (int i = 0; i < p; i++) {
      int sum = 0;
      for (int j = 0; j < s; j++) {
        if (v[i][j] == INF)
          c_hatena[j]++;
        else if (j != s - 1)
          sum += v[i][j];
      }
      r[i] = sum;
    }

    for (int i = 0; i < s; i++) {
      int sum = 0;
      for (int j = 0; j < p; j++) {
        if (v[j][i] == INF)
          r_hatena[j]++;
        else if (j != p - 1)
          sum += v[j][i];
      }
      c[i] = sum;
    }

    priority_queue<pair<bool, int>, vector<pair<bool, int>>,
                   greater<pair<bool, int>>>
        que;

    for (int i = 0; i < p; i++) {
      if (c_hatena[i] == 1) {
        que.push(pair<bool, int>(true, i));
      }
    }

    for (int i = 0; i < s; i++) {
      if (r_hatena[i] == 1) {
        que.push(pair<bool, int>(false, i));
      }
    }

    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v[i].size(); j++) {
        //	cout << v[i][j] << " ";
      }
      //      cout << endl;
    }

    // p tate s yoko
    // 0 is row 1 is column
    while (!que.empty()) {
      pair<bool, int> tmp = que.top();
      que.pop();

      if (tmp.first == 0) {
        if (r_hatena[tmp.second] <= 0)
          continue;

        for (int i = 0; i < s; i++) {
          if (v[tmp.second][i] == INF) {
            v[tmp.second][i] = v[tmp.second][s - 1] - r[tmp.second];
            //	    cout << v[tmp.second][s-1] << endl;
            //	    cout << r[tmp.second] << endl;
            r_hatena[tmp.second]--;
            c_hatena[i]--;
            //	    cout << tmp.second << " " << i << endl;
            total--;
            c[i] += v[tmp.second][i];
            if (c_hatena[i] == 1)
              que.push(pair<bool, int>(1, i));
          }
        }

      } else {
        if (c_hatena[tmp.second] <= 0)
          continue;
        for (int i = 0; i < p; i++) {
          if (v[i][tmp.second] == INF) {
            //	    cout << i << " " << tmp.second << endl;
            v[i][tmp.second] = v[p - 1][tmp.second] - c[tmp.second];
            c_hatena[tmp.second]--;
            r_hatena[i]--;
            //	    cout << i << " " << tmp.second << endl;
            //	    cout << c_hatena[tmp.second] << endl;
            //	    cout << r_hatena[i] << endl;
            r[i] += v[i][tmp.second];
            total--;
            if (r_hatena[i] == 1)
              que.push(pair<bool, int>(0, i));
            //	    cout << v[i][tmp.second] << endl;
          }
        }
      }
    }

    if (total <= 0) {
      for (int i = 0; i < pos.size(); i++) {
        int x = pos[i].first;
        int y = pos[i].second;
        cout << v[y][x] << endl;
      }
    } else
      cout << "NO" << endl;
  }
  return 0;
}