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

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

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int n;
int g[100][100];
int num[100];
bool f[100];
int d;
int dm;

void solve(int pos) {
  f[pos] = true;
  dm = max(dm, ++d);
  REP(i, num[pos]) {
    int next = g[pos][i];
    if (f[next])
      continue;
    solve(next);
  }
  f[pos] = false;
  --d;
}

int main() {
  while (n = getInt()) {
    REP(i, 100) {
      f[i] = false;
      num[i] = 0;
    }
    REP(i, n) {
      int a = getInt() - 1;
      int b = getInt() - 1;
      g[a][num[a]++] = b;
      g[b][num[b]++] = a;
    }
    dm = 0;
    REP(i, 100) { solve(i); }
    print(dm);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int n;
int g[100][100];
int num[100];
bool f[100];
int tmp[100];
int k;
int d;
int dm;

int solve(int pos) {
  int ans = 1;
  f[pos] = true;
  dm = max(dm, ++d);
  REP(i, num[pos]) {
    int next = g[pos][i];
    if (f[next])
      continue;
    // if(next < k && tmp[next] + d <= dm)
    // continue;
    ans = max(ans, 1 + solve(next));
  }
  f[pos] = false;
  d--;
  return ans;
}

int main() {
  while (n = getInt()) {
    dm = 0;
    REP(i, 100) {
      f[i] = false;
      num[i] = 0;
    }
    REP(i, n) {
      int a = getInt() - 1;
      int b = getInt() - 1;
      g[a][num[a]++] = b;
      g[b][num[b]++] = a;
    }
    int ans = 0;
    REP(i, 100) {
      k = i;
      d = 0;
      tmp[i] = solve(i);
      ans = max(ans, tmp[i]);
    }
    print(ans);
  }
  return 0;
}