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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  bool flag = false;
  while (1) {

    int H, W;
    cin >> H;
    if (!H)
      break;
    cin >> W;
    if (!flag) {
      flag = true;
    } else {
      cout << endl;
    }
    vector<int> rs(H + 1), cs(W + 1);
    int unknown = 0;
    map<pair<int, int>, int> mp;
    vector<vector<int>> field(H + 1, vector<int>(W + 1));
    for (int i = 0; i < H + 1; ++i) {
      for (int j = 0; j < W + 1; ++j) {
        string st;
        cin >> st;
        int num;
        if (st == "?") {
          num = 1e8;
          mp[make_pair(i, j)] = unknown++;
          rs[i]++;
          cs[j]++;
        } else
          num = stoi(st);
        field[i][j] = num;
      }
    }
    queue<pair<int, int>> que;
    for (int y = 0; y <= H; ++y) {
      if (rs[y] == 1) {
        que.push(make_pair(1, y));
      }
    }
    for (int x = 0; x <= W; ++x) {
      if (cs[x] == 1) {
        que.push(make_pair(0, x));
      }
    }
    while (!que.empty()) {
      auto p(que.front());
      que.pop();
      if (p.first) {
        const int y = p.second;
        if (rs[y] == 1) {
          int sum = 0;
          int place = -1;
          for (int i = 0; i <= W; ++i) {
            if (field[y][i] != 1e8) {
              sum += i != W ? field[y][i] : -field[y][i];
            } else {
              place = i;
            }
          }
          if (place != W) {
            field[y][place] = -sum;
          } else {
            field[y][place] = sum;
          }
          rs[y]--;
          cs[place]--;
          if (cs[place] == 1) {
            que.push(make_pair(0, place));
          }
        }
      } else {
        const int x = p.second;
        if (cs[x] == 1) {
          int sum = 0;
          int place = -1;
          for (int i = 0; i <= H; ++i) {
            if (field[i][x] != 1e8) {
              sum += i != H ? field[i][x] : -field[i][x];
            } else {
              place = i;
            }
          }
          if (place != H) {
            field[place][x] = -sum;
          } else {
            field[place][x] = sum;
          }
          cs[x]--;
          rs[place]--;
          if (rs[place] == 1) {
            que.push(make_pair(1, place));
          }
        }
      }
    }
    bool ok = true;
    for (int i = 0; i <= H; ++i) {
      int sum = 0;
      for (int j = 0; j < W; ++j) {
        sum += field[i][j];
      }
      if (sum != field[i][W])
        ok = false;
    }
    for (int i = 0; i <= W; ++i) {
      int sum = 0;
      for (int j = 0; j < H; ++j)
        sum += field[j][i];
      if (sum != field[H][i])
        ok = false;
    }
    if (!ok)
      cout << "NO" << endl;
    else {
      for (auto m : mp) {
        cout << field[m.first.first][m.first.second] << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  bool flag = false;
  while (1) {
    if (!flag) {
      flag = true;
    } else {
      cout << endl;
    }
    int H, W;
    cin >> H;
    if (!H)
      break;
    cin >> W;
    vector<int> rs(H), cs(W);
    int unknown = 0;
    map<pair<int, int>, int> mp;
    vector<vector<int>> field(H + 1, vector<int>(W + 1));
    for (int i = 0; i < H + 1; ++i) {
      for (int j = 0; j < W + 1; ++j) {
        string st;
        cin >> st;
        int num;
        if (st == "?") {
          num = 1e8;
          mp[make_pair(i, j)] = unknown++;
          rs[i]++;
          cs[j]++;
        } else
          num = stoi(st);
        field[i][j] = num;
      }
    }
    queue<pair<int, int>> que;
    for (int y = 0; y <= H; ++y) {
      if (rs[y] == 1) {
        que.push(make_pair(1, y));
      }
    }
    for (int x = 0; x <= W; ++x) {
      if (cs[x] == 1) {
        que.push(make_pair(0, x));
      }
    }
    while (!que.empty()) {
      auto p(que.front());
      que.pop();
      if (p.first) {
        const int y = p.second;
        if (rs[y] == 1) {
          int sum = 0;
          int place = -1;
          for (int i = 0; i <= W; ++i) {
            if (field[y][i] != 1e8) {
              sum += i != W ? field[y][i] : -field[y][i];
            } else {
              place = i;
            }
          }
          if (place != W) {
            field[y][place] = -sum;
          } else {
            field[y][place] = sum;
          }
          rs[y]--;
          cs[place]--;
          if (cs[place] == 1) {
            que.push(make_pair(0, place));
          }
        }
      } else {
        const int x = p.second;
        if (cs[x] == 1) {
          int sum = 0;
          int place = -1;
          for (int i = 0; i <= H; ++i) {
            if (field[i][x] != 1e8) {
              sum += i != H ? field[i][x] : -field[i][x];
            } else {
              place = i;
            }
          }
          if (place != H) {
            field[place][x] = -sum;
          } else {
            field[place][x] = sum;
          }
          cs[x]--;
          rs[place]--;
          if (rs[place] == 1) {
            que.push(make_pair(1, place));
          }
        }
      }
    }
    bool ok = true;
    for (int i = 0; i <= H; ++i) {
      int sum = 0;
      for (int j = 0; j < W; ++j) {
        sum += field[i][j];
      }
      if (sum != field[i][W])
        ok = false;
    }
    for (int i = 0; i <= W; ++i) {
      int sum = 0;
      for (int j = 0; j < H; ++j)
        sum += field[j][i];
      if (sum != field[H][i])
        ok = false;
    }
    if (!ok)
      cout << "NO" << endl;
    else {
      for (auto m : mp) {
        cout << field[m.first.first][m.first.second] << endl;
      }
    }
  }
  return 0;
}