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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

vector<int> edge[100];
bool used_once[100];
bool used[100];
int dfs(int v) {
  used_once[v] = true;
  used[v] = true;
  int res = 0;
  foreach (it, edge[v])
    if (!used[*it])
      res = max(res, dfs(*it));
  used[v] = false;
  return res + 1;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < 100; ++i)
      edge[i] = vector<int>();
    while (n--) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a;
      --b;
      edge[a].push_back(b);
      edge[b].push_back(a);
    }

    pint e[100];
    for (int i = 0; i < 100; ++i)
      e[i] = pint(edge[i].size(), i);
    sort(e, e + 100);

    int res = 0;
    int last = 9999;
    fill(used_once, used_once + 100, false);
    for (int i = 0; i < 100; ++i) {
      if (!used_once[e[i].second] || e[i].first == last) {
        res = max(res, dfs(e[i].second));
        last = e[i].first;
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

vector<int> edge[100];
bool once_used[100];
bool used[100];
int dfs(int v) {
  once_used[v] = true;
  used[v] = true;
  int res = 0;
  foreach (it, edge[v])
    if (!used[*it])
      res = max(res, dfs(*it));
  used[v] = false;
  return res + 1;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < 100; ++i)
      edge[i] = vector<int>();
    fill(once_used, once_used + 100, false);
    while (n--) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a;
      --b;
      edge[a].push_back(b);
      edge[b].push_back(a);
    }

    int res = 0;
    for (int i = 0; i < 100; ++i)
      res = max(res, dfs(i));
    printf("%d\\n", res);
  }
}