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

#include <iostream>
#include <utility>
#define P pair<int, int>
#define U 1000
using namespace std;

int n;
P p[200][200];

bool solve(int x, int y, P org, int k) {
  if (k > n * n)
    return false;
  if (p[x][y].first == U)
    return false;
  if (p[x][y].first == org.first && p[x][y].second == org.second)
    return true;
  //  cout << "  s"<<x<<","<<y<< endl;

  int nx = p[x][y].first, ny = p[x][y].second;
  return solve(nx, ny, org, k + 1);
}
void loop(int x, int y) {
  if (p[x][y].first == U)
    return;
  int nx = p[x][y].first, ny = p[x][y].second;
  p[x][y].first = U;
  p[x][y].second = U;
  loop(nx, ny);
}

void pr() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j].first == U)
        cout << " *,*";
      else
        cout << " " << p[i][j].first << "," << p[i][j].second;
    }
    cout << endl;
  }
}

int main() {

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        cin >> p[j][i].first >> p[j][i].second;
    }
    //    cout << "------------------------------" << endl;
    //    pr(); cout << endl;

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (p[i][j].first == U)
          continue;

        if (solve(p[i][j].first, p[i][j].second, p[i][j], 0)) {
          loop(i, j); // erase loop
          //	  pr();cout << " ans++ by " << i << " " << j << endl;
          ans++;
        } else {
          p[i][j].first = U;
          p[i][j].second = U;
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#define P pair<int, int>
#define U 1000
using namespace std;

int n;
P p[200][200];

bool solve(int x, int y, P org) {
  if (p[x][y].first == U)
    return false;
  if (p[x][y].first == org.first && p[x][y].second == org.second)
    return true;
  //  cout << "  s"<<x<<","<<y<< endl;

  int nx = p[x][y].first, ny = p[x][y].second;
  return solve(nx, ny, org);
}
void loop(int x, int y) {
  if (p[x][y].first == U)
    return;
  int nx = p[x][y].first, ny = p[x][y].second;
  p[x][y].first = U;
  p[x][y].second = U;
  loop(nx, ny);
}

void pr() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j].first == U)
        cout << " *,*";
      else
        cout << " " << p[i][j].first << "," << p[i][j].second;
    }
    cout << endl;
  }
}

int main() {

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        cin >> p[j][i].first >> p[j][i].second;
    }
    //    cout << "------------------------------" << endl;
    //    pr(); cout << endl;

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (p[i][j].first == U)
          continue;

        if (solve(p[i][j].first, p[i][j].second, p[i][j])) {
          loop(i, j); // erase loop
          //	  pr();cout << " ans++ by " << i << " " << j << endl;
          ans++;
        } else {
          p[i][j].first = U;
          p[i][j].second = U;
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}