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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

// map<int, int>m;
vector<vector<int>> G(MAX_N);

int dfs(int now, int visits[], int c) {
  int res = c;
  rep(i, G[now].size()) {
    //    if(m.count(G[now][i])) continue;
    if (visits[G[now][i]])
      continue;
    //    m[G[now][i]] = 1;
    visits[G[now][i]] = 1;
    res = max(res, dfs(G[now][i], visits, c + 1));
    //    m.erase(G[now][i]);
    visits[G[now][i]] = 0;
  }
  return res;
}

int main() {
  int n;
  while (scanf("%d", &n) && n) {
    rep(i, n) G[i].clear();
    int a, b;
    vi v;
    rep(i, n) {
      scanf("%d%d", &a, &b);
      a--;
      b--;
      G[a].PB(b);
      G[b].PB(a);
      if (find(v.begin(), v.end(), a) == v.end())
        v.PB(a);
      if (find(v.begin(), v.end(), b) == v.end())
        v.PB(b);
    }
    int res = 0;
    int visits[MAX_N] = {};
    rep(i, v.size()) {
      //      m.clear();
      //      m[v[i]] = 1;
      visits[v[i]] = 1;
      res = max(res, dfs(v[i], visits, 1));
      visits[v[i]] = 0;
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

map<int, int> m;
vector<vector<int>> G(MAX_N);
int ans = 0;

void dfs(int now, int c) {
  c++;
  int res = c;
  rep(i, G[now].size()) {
    if (m.count(G[now][i]))
      continue;
    m[G[now][i]] = 1;
    dfs(G[now][i], c);
    m.erase(G[now][i]);
  }
  ans = max(ans, c);
}

int main() {
  int n;
  while (scanf("%d", &n) && n) {
    rep(i, n) G[i].clear();
    int a, b;
    vi v;
    rep(i, n) {
      scanf("%d%d", &a, &b);
      a--;
      b--;
      G[a].PB(b);
      G[b].PB(a);
      if (find(v.begin(), v.end(), a) == v.end())
        v.PB(a);
      if (find(v.begin(), v.end(), b) == v.end())
        v.PB(b);
    }
    int res = 0;
    rep(i, v.size()) {
      m.clear();
      m[v[i]] = 1;
      dfs(v[i], 0);
    }
    printf("%d\\n", ans);
  }
  return 0;
}