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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> G[101];
int ans = 0;
int mn[101];
bool vis[101];
bool nuse[101];
int INF = 10000;
vector<int> group;

int dfs(int v) {
  vis[v] = true;
  int res = 0;
  for (int i = 0; i < G[v].size(); i++) {
    if (vis[G[v][i]])
      continue;
    vis[G[v][i]] = true;
    res = max(res, dfs(G[v][i]));
    vis[G[v][i]] = false;
  }
  return res + 1;
}

int searchmin(int v) {
  int res = G[v].size();
  vis[v] = true;
  group.push_back(v);
  for (int i = 0; i < G[v].size(); i++) {
    if (!vis[G[v][i]])
      res = min(res, searchmin(G[v][i]));
  }
  return res;
}

int main(void) {
  while (1) {
    for (int i = 0; i < 101; i++)
      G[i].clear(), mn[i] = INF;
    memset(nuse, false, sizeof(nuse));
    ans = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    for (int i = 0; i < 101; i++) {
      if (mn[i] != INF)
        continue;
      group.clear();
      memset(vis, false, sizeof(vis));
      int mini = searchmin(i);
      for (int j = 0; j < group.size(); j++)
        mn[group[j]] = mini;
    }
    for (int i = 0; i < 101; i++) {
      if (G[i].size() == mn[i]) {
        memset(vis, false, sizeof(vis));
        ans = max(ans, dfs(i));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int f[101], t[101];
int ans = 0;
int nf[101], nt[101];
bool use[101];
bool nuse[101];

void dfs(int x, int y, int res, bool usen[101], bool nusen[101]) {
  ans = max(ans, res);
  for (int i = nf[x]; i <= nt[y]; i++) {
    if (usen[i] == false) {
      usen[i] = true;
      if (x == f[i] && nusen[t[i]] == false) {
        nusen[t[i]] = true;
        dfs(min(t[i], y), max(t[i], y), res + 1, usen, nusen);
        nusen[t[i]] = false;
      }
      if (x == t[i] && nusen[f[i]] == false) {
        nusen[f[i]] = true;
        dfs(min(f[i], y), max(f[i], y), res + 1, usen, nusen);
        nusen[f[i]] = false;
      }
      if (y == f[i] && nusen[t[i]] == false) {
        nusen[t[i]] = true;
        dfs(min(x, t[i]), max(x, t[i]), res + 1, usen, nusen);
        nusen[t[i]] = false;
      }
      if (y == t[i] && nusen[f[i]] == false) {
        nusen[f[i]] = true;
        dfs(min(x, f[i]), max(x, f[i]), res + 1, usen, nusen);
        nusen[f[i]] = false;
      }
      if (x == f[i] && nusen[t[i]] == true)
        ans = max(ans, res + 1);
      if (x == t[i] && nusen[f[i]] == true)
        ans = max(ans, res + 1);
      if (y == f[i] && nusen[t[i]] == true)
        ans = max(ans, res + 1);
      if (y == t[i] && nusen[f[i]] == true)
        ans = max(ans, res + 1);
      usen[i] = false;
    }
  }
}

int main(void) {
  while (1) {
    memset(use, false, sizeof(use));
    memset(nuse, false, sizeof(nuse));
    ans = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &f[i], &t[i]);
    }
    for (int i = 0; i < n; i++)
      nf[i] = 99, nt[i] = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        if (f[i] < f[j] || (f[i] == f[j] && t[i] < t[j])) {
          swap(f[i], f[j]);
          swap(t[i], t[j]);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      nf[f[i]] = min(i, nf[f[i]]);
      nf[t[i]] = min(i, nf[t[i]]);
      nt[f[i]] = max(i, nt[f[i]]);
      nt[t[i]] = max(i, nt[t[i]]);
    }
    for (int i = 0; i < n; i++) {
      use[i] = true;
      nuse[f[i]] = true;
      nuse[t[i]] = true;
      dfs(f[i], t[i], 1, use, nuse);
      use[i] = false;
      nuse[f[i]] = false;
      nuse[t[i]] = false;
    }
    cout << ans << endl;
  }
  return 0;
}