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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[8] = { //?§????
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int n;
vector<vector<int>> g;
int used[1000];

int func(int cur) {
  int res = 0;
  used[cur] = 1;
  for (auto itr : g[cur]) {
    if (!used[itr]) {
      res = max(res, 1 + func(itr));
    }
  }
  used[cur] = 0;
  return res;
}

signed main() {

  while (scnaf("%d", &n) != EOF) {
    if (!n)
      break;
    g.clear();
    g.resize(n * 2);
    map<int, int> v;
    int vc = 0;
    REP(i, n) {
      int a, b;
      scanf("%d %d", &a, &b);
      if (!v.count(a))
        v[a] = v.size();
      if (!v.count(b))
        v[b] = v.size();
      g[v[a]].push_back(v[b]);
      g[v[b]].push_back(v[a]);
    }
    int ans = 0;
    REP(i, v.size()) { ans = max(ans, func(i)); }
    printf("%d\\n", ans + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[8] = { //?§????
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int n;
vector<vector<int>> g;
set<int> used;

int func(int cur) {
  int res = 0;
  used.insert(cur);
  for (auto itr : g[cur]) {
    if (used.find(itr) == used.end()) {
      res = max(res, 1 + func(itr));
    }
  }
  used.erase(cur);
  return res;
}

signed main() {

  while (scnaf("%d", &n) != EOF) {
    if (!n)
      break;
    g.clear();
    g.resize(n * 2);
    map<int, int> v;
    int vc = 0;
    REP(i, n) {
      int a, b;
      scanf("%d %d", &a, &b);
      if (!v.count(a))
        v[a] = v.size();
      if (!v.count(b))
        v[b] = v.size();
      g[v[a]].push_back(v[b]);
      g[v[b]].push_back(v[a]);
    }
    int ans = 0;
    REP(i, v.size()) { ans = max(ans, func(i)); }
    printf("%d\\n", ans + 1);
  }
  return 0;
}