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

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int ice[92][92];

int dfs(int x, int y) {
  int dx[4] = {-1, 0, 0, 1};
  int dy[4] = {0, -1, 1, 0};
  int a = 0;

  REP(i, 4) {
    if (ice[x + dx[i]][y + dy[i]] == 1) {
      ice[x + dx[i]][y + dy[i]] = 0;
      a = max(a, dfs(x + dx[i], y + dy[i]));
      ice[x + dx[i]][y + dy[i]] = 1;
    }
  }
  if (a == 0)
    return 1;
  return a + 1;
}

int main(void) {
  int n, m;
  while (cin >> m >> n && m && n) {
    int res = 0;
    REP(i, 92) { fill(ice[i], ice[i] + 92, 0); }
    REP(i, n) {
      REP(j, m) { cin >> ice[i + 1][j + 1]; }
    }
    REP(i, n) {
      REP(j, m) {
        if (ice[i + 1][j + 1] == 1) {
          ice[i + 1][j + 1] = 0;
          res = max(res, dfs(i + 1, j + 1));
          ice[i + 1][j + 1] = 1;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int dfs(vector<vector<int>> ice, int x, int y) {
  int dx[4] = {-1, 0, 0, 1};
  int dy[4] = {0, -1, 1, 0};
  int max = 0;

  REP(i, 4) {
    if (ice[x + dx[i]][y + dy[i]] == 1) {
      ice[x][y] = 0;
      max = dfs(ice, x + dx[i], y + dy[i]);
      ice[x][y] = 1;
    }
  }
  if (max == 0)
    return 1;
  return max + 1;
}

int main(void) {
  int n, m;
  while (cin >> m >> n && m && n) {
    vector<vector<int>> ice(92, vector<int>(92));
    int res = 0;
    REP(i, 92) { fill(ice[i].begin(), ice[i].end(), 0); }
    REP(i, n) {
      REP(j, m) { cin >> ice[i + 1][j + 1]; }
    }
    REP(i, n) {
      REP(j, m) {
        if (ice[i + 1][j + 1] == 1) {
          res = max(res, dfs(ice, i + 1, j + 1));
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}