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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

vector<int> G[100];
bool visited[100];
int memo[100];

int visit(int v) {
  int res = 0;
  rep(i, (int)G[v].size()) {
    if (!visited[G[v][i]] && memo[G[v][i]] >= res) {
      visited[G[v][i]] = true;
      res = max(res, 1 + visit(G[v][i]));
      visited[G[v][i]] = false;
    }
  }
  return res;
}

int main() {
  int n;
  int a, b;
  while (cin >> n && n) {

    rep(i, 100) G[i].clear();
    memset(visited, false, sizeof(visited));
    fill(memo, memo + 100, INF);

    rep(i, n) {
      cin >> a >> b;
      a--, b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }

    int res = 0;

    rep(i, 100) {
      visited[i] = true;
      memo[i] = visit(i);
      res = max(res, memo[i]);
      visited[i] = false;
    }
    cout << res + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

int G[100][100];
int sz[100];
bool visited[100];

int visit(int v) {
  int res = 0;
  rep(i, sz[v]) {
    if (!visited[G[v][i]]) {
      visited[G[v][i]] = true;
      res = max(res, 1 + visit(G[v][i]));
      visited[G[v][i]] = false;
    }
  }
  return res;
}

int main() {
  int n;
  int a, b;
  while (cin >> n && n) {
    memset(sz, 0, sizeof(sz));
    memset(visited, false, sizeof(visited));

    rep(i, n) {
      cin >> a >> b;
      a--, b--;
      G[a][sz[a]++] = b;
      G[b][sz[b]++] = a;
    }

    int res = 0;

    rep(i, 100) {
      visited[i] = true;
      res = max(res, visit(i));
      visited[i] = false;
    }
    cout << res + 1 << endl;
  }
  return 0;
}