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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int R, C;
  bool f = false;
  while (cin >> R >> C) {
    if (f)
      cout << endl;
    else
      f = true;
    int data[101][101];
    int cntx[101] = {};
    int cnty[101] = {};
    vector<P> fout;
    REP(y, R + 1) REP(x, C + 1) {
      string s;
      cin >> s;
      if (s == "?") {
        data[y][x] = -INF;
        fout.push_back(P(x, y));
        cntx[x]++;
        cnty[y]++;
      } else {
        data[y][x] = atoi(s.c_str());
      }
    }
    while (true) {
      bool updata = false;
      REP(y, R + 1) {
        if (cnty[y] == 1) {
          updata = true;
          int sum = 0;
          REP(x, C + 1)
          if (data[y][x] != -INF) sum += ((x == C) ? 1 : -1) * data[y][x];
          REP(x, C + 1) if (data[y][x] == -INF) {
            data[y][x] = ((x == C) ? -1 : 1) * sum;
            cntx[x]--;
            cnty[y]--;
          }
        }
      }
      REP(x, C + 1) {
        if (cntx[x] == 1) {
          cntx[x] = 0;
          updata = true;
          int sum = 0;
          REP(y, R + 1)
          if (data[y][x] != -INF) sum += ((y == R) ? 1 : -1) * data[y][x];
          REP(y, R + 1) if (data[y][x] == -INF) {
            data[y][x] = ((y == R) ? -1 : 1) * sum;
            cntx[x]--;
            cnty[y]--;
          }
        }
      }
      if (!updata)
        break;
    }
    int cnt = 0;
    REP(y, R + 1) REP(x, C + 1) if (data[y][x] == -INF) cnt++;
    if (cnt > 0)
      cout << "NO" << endl;
    else {
      REP(i, fout.size()) {
        cout << data[fout[i].second][fout[i].first] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int R, C;
  while (cin >> R >> C) {
    int data[101][101];
    vector<P> fout;
    REP(y, R + 1) REP(x, C + 1) {
      string s;
      cin >> s;
      if (s == "?") {
        data[y][x] = -1;
        fout.push_back(P(x, y));
      } else {
        data[y][x] = atoi(s.c_str());
      }
    }
    while (true) {
      bool updata = false;
      int ndata[101][101];
      memcpy(ndata, data, sizeof(data));
      REP(y, R + 1) {
        int cnt = 0;
        REP(x, C + 1) if (data[y][x] == -1) cnt++;
        if (cnt == 1) {
          updata = true;
          int sum = 0;
          REP(x, C + 1)
          if (data[y][x] != -1) sum += ((x == C) ? 1 : -1) * data[y][x];
          REP(x, C + 1)
          if (data[y][x] == -1) ndata[y][x] = ((x == C) ? -1 : 1) * sum;
        }
      }
      REP(x, C + 1) {
        int cnt = 0;
        REP(y, R + 1) if (data[y][x] == -1) cnt++;
        if (cnt == 1) {
          updata = true;
          int sum = 0;
          REP(y, R + 1)
          if (data[y][x] != -1) sum += ((y == R) ? 1 : -1) * data[y][x];
          REP(y, R + 1)
          if (data[y][x] == -1) ndata[y][x] = ((y == R) ? -1 : 1) * sum;
        }
      }
      memcpy(data, ndata, sizeof(data));
      if (!updata)
        break;
    }
    int cnt = 0;
    REP(y, R + 1) REP(x, C + 1) if (data[y][x] == -1) cnt++;
    if (cnt > 0)
      cout << "NO" << endl;
    else {
      REP(i, fout.size()) {
        cout << data[fout[i].second][fout[i].first] << endl;
      }
    }
    cout << endl;
  }
  return 0;
}