alexjercan/bugnet huggingface dataset:
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool used[105];
vector<int> edge[105];
int n, a, b;

int dfs(int x) {
  if (edge[x].empty())
    return 0;
  if (used[x])
    return 0;
  int res = 0;
  used[x] = true;
  for (int i = 0; i < edge[x].size(); i++) {
    res = max(res, dfs(edge[x][i]));
  }
  used[x] = false;
  return res + 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      return 0;
    for (int i = 0; i < 100; i++) {
      edge[i].clear();
    }
    for (int i = 0; i < n; i++) {
      cin >> 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));
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

int rings[200];
int neib[101][200];
int used[101];
int onceused[101];

int dfs(int x) {
  used[x] = 1;
  onceused[x] = 1;
  int res = 1;
  for (int i = 1; i <= neib[x][0]; i++) {
    if (used[neib[x][i]] == 0) {
      res = max(res, 1 + dfs(neib[x][i]));
    }
  }
  used[x] = 0;
  return res;
}

int main() {
  while (1) {
    int n, ans = 0, mind = 10000;
    rings[0] = 0;
    for (int i = 0; i < 101; i++)
      neib[i][0] = 0;
    scanf("%d", &n);
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      rings[++rings[0]] = a;
      rings[++rings[0]] = b;
      neib[a][++neib[a][0]] = b;
      neib[b][++neib[b][0]] = a;
    }
    memset(onceused, 0, sizeof(onceused));
    memset(used, 0, sizeof(used));
    for (int i = 1; i < 101; i++) {
      for (int j = 1; j <= rings[0]; j++) {
        if (neib[rings[j]][0] == i) {
          ans = max(ans, dfs(rings[j]));
        }
      }
      bool fin = true;
      for (int j = 1; j <= rings[0]; j++) {
        if (onceused[rings[j]] == 0) {
          fin = false;
          break;
        }
      }
      if (fin)
        break;
    }
    printf("%d\\n", ans);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[100];
int used[100];
int solve(int a) {
  used[a] = 1;
  int ret = 1;
  for (int i = 0; i < g[a].size(); i++) {
    if (!used[g[a][i]]) {
      ret = max(ret, 1 + solve(g[a][i]));
    }
  }
  used[a] = 0;
  return ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 100; i++)
      g[i].clear();
    for (int i = 0; i < a; i++) {
      int b, c;
      scanf("%d%d", &b, &c);
      b--;
      c--;
      g[b].push_back(c);
      g[c].push_back(b);
    }
    int ret = 0;
    for (int i = 0; i < 100; i++)
      ret = max(ret, solve(i));
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
bool visited[128];
vector<int> g[128];
int solve(int a) {
  visited[a] = true;
  int ret = 0;
  int i;
  int c = g[a].size();
  for (i = 0; i < c; ++i) {
    if (!visited[g[a][i]])
      ret = max(ret, solve(g[a][i]));
  }
  visited[a] = false;
  return ret + 1;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 100; ++i)
      g[i] = vector<int>(0);
    for (int i = 0; i < a; ++i) {
      int b, c;
      scanf("%d%d", &b, &c);
      g[b - 1].push_back(c - 1);
      g[c - 1].push_back(b - 1);
    }
    int ret = 0;
    // for(int i=0;i<100;i++)size[i]=g[i].size();
    for (int i = 0; i < 100; ++i)
      ret = max(ret, solve(i));
    printf("%d\\n", ret);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int n, used[100], e[100];
vector<int> v[100];

int f(int from) {
  int res = 0;
  rep(i, v[from].size()) {
    int to = v[from][i];
    if (!used[to]) {
      used[to] = 1;
      res = max(res, f(to) + 1);
      used[to] = 0;
    }
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    fill(used, used + 100, 0);
    fill(e, e + 100, 0);
    rep(i, 100) v[i].clear();
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      v[a].pb(b);
      v[b].pb(a);
    }
    rep(i, 100) sort(v[i].begin(), v[i].end());
    int done[100];
    fill(done, done + 100, 0);
    rep(i, 100) if (!done[i]) {
      int x = 0;
      rep(j, 100) if (v[i] == v[j]) {
        x++;
        done[j] = 1;
      }
      if (x > v[i].size() + 1) {
        int c = 0;
        rep(j, 100) if (v[i] == v[j]) {
          c++;
          if (c > v[i].size() + 1)
            used[j] = 1;
        }
      }
      done[i] = 1;
    }
    int ans = 2;
    rep(i, 100) if (!used[i]) {
      rep(j, v[i].size()) if (!used[v[i][j]]) {
        used[i] = used[v[i][j]] = 1;
        ans = max(ans, f(v[i][j]) + 2);
        used[i] = used[v[i][j]] = 0;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int n, used[100];
vector<int> v[100];

int f(int from) {
  int res = 0;
  rep(i, v[from].size()) {
    int to = v[from][i];
    if (!used[to]) {
      used[to] = 1;
      res = max(res, f(to) + 1);
      used[to] = 0;
    }
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    fill(used, used + n, 0);
    rep(i, 100) v[i].clear();
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      v[a].pb(b);
      v[b].pb(a);
    }
    int ans = 2;
    rep(i, 100) {
      rep(j, v[i].size()) {
        used[i] = used[v[i][j]] = 1;
        ans = max(ans, f(v[i][j]) + 2);
        used[i] = used[v[i][j]] = 0;
      }
    }
    cout << ans << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <vector>
using namespace std;
vector<int> e[100];
bool used[100];
int max(int a, int b) { return a > b ? a : b; }
int dfs(int f, int t) {
  int i;
  int m = 0;
  for (i = 0; i < e[t].size(); i++) {
    if (e[t][i] == f)
      continue;
    if (!used[e[t][i]]) {
      used[t] = true;
      m = max(m, dfs(t, e[t][i]));
      used[t] = false;
    }
  }
  return m + 1;
}
int main() {
  int n;
  int i, j;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    for (i = 0; i < 100; i++)
      e[i].clear();
    for (i = 0; i < n; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      a--;
      b--;
      e[a].push_back(b);
      e[b].push_back(a);
    }
    int ans = 0;
    for (i = 0; i < 100; i++)
      used[i] = false;
    for (i = 0; i < 100; i++) {
      for (j = 0; j < e[i].size(); j++) {
        used[i] = true;
        ans = max(ans, dfs(i, e[i][j]) + 1);
        used[i] = false;
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int n;
int i, j;
int edge[100][100];
typedef struct {
  int from;
  int to;
} t_edge;
t_edge edge2[100];
int c[100];
int used[100];
int m;

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int solve(int from, int to) {
  used[from] = 1;
  int x = 0;
  for (int k = 0; k < c[to]; k++) {
    if (used[edge[to][k]] != 1) {
      x = max(x, solve(to, edge[to][k]));
    }
  }
  return x + 1;
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    m = 0;
    for (i = 0; i < 100; i++)
      for (j = 0; j < 100; j++)
        edge[i][j] = 0;
    for (i = 0; i < 100; i++)
      c[i] = 0;
    for (i = 0; i < 100; i++)
      used[i] = 0;
    for (i = 0; i < n; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      edge[a][c[a]++] = b;
      edge[b][c[b]++] = a;
      edge2[i].from = a;
      edge2[i].to = b;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < 100; j++)
        used[j] = 0;
      m = max(m, solve(edge2[i].from, edge2[i].to));
      for (j = 0; j < 100; j++)
        used[j] = 0;
      m = max(m, solve(edge2[i].to, edge2[i].from));
    }
    printf("%d\\n", m + 1);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back

vector<vector<int>> G(100);
typedef bitset<100> bs;

int dfs(int v, bs vis) {
  vis[v] = true;
  int res = 1;
  rep(i, G[v].size()) {
    int to = G[v][i];
    if (!vis[to]) {
      res = max(res, dfs(to, vis) + 1);
    }
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, 100) G[i].clear();
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    bs init;
    int ans = 1;
    rep(i, 100) { ans = max(dfs(i, init), ans); }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back

vector<vector<int>> G(100);
typedef bitset<100> bs;
map<pair<int, bs>, int> dp;

namespace std {
bool operator<(const bs &a, const bs &b) {
  rep(i, 100) {
    if (a[i] != b[i])
      return a[i] < b[i];
  }
  return false;
}
} // namespace std

int dfs(int v, bs vis) {
  auto it = dp.find(make_pair(v, vis));
  if (it != dp.end())
    return it->second;

  vis[v] = true;
  int res = 1;
  rep(i, G[v].size()) {
    int to = G[v][i];
    if (!vis[to]) {
      res = max(res, dfs(to, vis) + 1);
    }
  }
  dp[make_pair(v, vis)] = res;
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, 100) G[i].clear();
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    bs init;
    int ans = 1;
    rep(i, 100) { ans = max(dfs(i, init), ans); }
    cout << ans << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi adj;
int ans, cnt;
bool visited[100];

void dfs(int u) {
  if (ans < cnt)
    ans = cnt;

  cnt++;
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    if (!visited[v]) {
      visited[v] = true;
      dfs(v);
      visited[v] = false;
    }
  }
  cnt--;
}

int main() {
  for (int m; scanf("%d", &m), m;) {
    bool adjm[100][100] = {}, flg[100] = {};
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      adjm[a][b] = adjm[b][a] = flg[a] = flg[b] = true;
    }

    int f[100], n = 0;
    rep(i, 100) if (flg[i]) f[i] = n++;

    adj = vvi(n);
    rep(i, 100) rep(j, 100) if (adjm[i][j]) adj[f[i]].pb(f[j]);

    ans = 0, cnt = 1;
    rep(u, n - 1) {
      visited[u] = true;
      dfs(u);
      visited[u] = false;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi adj;
int n, ans;
bool visited[100];

void dfs(int u, int cnt) {
  if (ans < cnt)
    ans = cnt;

  rep(i, adj[u].size()) {
    int v = adj[u][i];
    if (!visited[v]) {
      visited[v] = true;
      dfs(v, cnt + 1);
      visited[v] = false;
    }
  }
}

int main() {
  for (int m; scanf("%d", &m), m;) {
    bool adjm[100][100] = {}, flg[100] = {};
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      adjm[a][b] = adjm[b][a] = flg[a] = flg[b] = true;
    }

    int f[100];
    n = 0;
    rep(i, 100) if (flg[i]) f[i] = n++;

    adj = vvi(n);
    rep(i, 100) rep(j, 100) if (adjm[i][j]) adj[f[i]].pb(f[j]);

    ans = 0;
    rep(u, n) visited[u] = false;
    rep(u, n - 1) {
      visited[u] = true;
      dfs(u, 1);
      visited[u] = false;
    }
    printf("%d\\n", ans);
  }

  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int maxcnt;
bool vis[100];
int graph_lens[100];
int graph[100][100];

void search(int i, int cnt, int ub) {
  if (maxcnt < cnt)
    maxcnt = cnt;
  vis[i] = true;
  int len = graph_lens[i];
  int *edg = graph[i];
  ub++;
  for (int j = 0; j < len; j++) {
    if (!vis[edg[j]])
      ub--;
  }
  if (maxcnt < ub) {
    cnt++;
    for (int j = 0; j < len; j++) {
      int to = edg[j];
      if (!vis[to])
        search(to, cnt, ub);
    }
  }
  vis[i] = false;
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    fill(vis, vis + 100, false);
    fill(graph_lens, graph_lens + 100, 0);
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      graph[a][graph_lens[a]++] = b;
      graph[b][graph_lens[b]++] = a;
    }
    maxcnt = 0;
    for (int i = 0; i < 100; i++) {
      search(i, 1, n + 1);
    }
    printf("%d\\n", maxcnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int maxcnt;
bool vis[100];
int graph_lens[100];
int graph[100][100];

void search(int i, int cnt) {
  if (vis[i])
    return;
  if (maxcnt < cnt)
    maxcnt = cnt;
  vis[i] = true;
  int len = graph_lens[i];
  int *edg = graph[i];
  for (int i = 0; i < len; i++) {
    int to = edg[i];
    search(to, cnt + 1);
  }
  vis[i] = false;
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    fill(vis, vis + 100, false);
    fill(graph_lens, graph_lens + 100, 0);
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      graph[a][graph_lens[a]++] = b;
      graph[b][graph_lens[b]++] = a;
    }
    maxcnt = 0;
    for (int i = 0; i < 100; i++) {
      search(i, 1);
    }
    printf("%d\\n", maxcnt);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
using namespace std;
int n;
bool used[100];
bool notused[100];
vector<int> G[100];
int dfs(int x, int cnt) {
  int res = cnt;
  used[x] = true;
  notused[x] = false;
  for (int i = 0; i < G[x].size(); i++) {
    int y = G[x][i];
    if (used[y])
      continue;
    res = max(res, dfs(y, cnt + 1));
  }
  used[x] = false;
  return res;
}
int main() {
  while (scanf("%d", &n)) {
    if (!n)
      break;
    memset(used, 0, sizeof(used));
    memset(notused, 0, sizeof(notused));
    for (int i = 0; i < 100; i++)
      G[i].clear();
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
      notused[a - 1] = notused[b - 1] = true;
    }
    int res = 0;
    for (int j = 1;; j++) {
      for (int i = 0; i < 100; i++) {
        if (G[i].size() != j)
          continue;
        res = max(res, dfs(i, 1));
      }
      bool isbreak = true;
      for (int i = 0; i < 100; i++) {
        if (notused[i]) {
          isbreak = false;
          break;
        }
      }
      if (isbreak)
        break;
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
struct chain {
  int a, b;
};
int n;
set<int> se;
deque<int> deq;
chain c[100];
bool used[100];
int cnt() {
  se.clear();
  for (int i = 0; i < deq.size(); i++)
    se.insert(deq[i]);
  return se.size();
}
int dfs() {
  int res = cnt();
  int s = deq[0], t = deq[deq.size() - 1];
  //    for (int i=0; i<res; i++) printf("%d%c",deq[i],i==res-1?'\\n':' ');
  for (int i = 0; i < n; i++) {
    if (used[i])
      continue;
    if (t == c[i].a && se.find(c[i].b) == se.end()) {
      used[i] = true;
      deq.push_back(c[i].b);
      res = max(res, dfs());
      deq.pop_back();
      used[i] = false;
    } else if (t == c[i].b && se.find(c[i].a) == se.end()) {
      used[i] = true;
      deq.push_back(c[i].a);
      res = max(res, dfs());
      deq.pop_back();
      used[i] = false;
    }
    if (s == c[i].a && se.find(c[i].b) == se.end()) {
      used[i] = true;
      deq.push_front(c[i].b);
      res = max(res, dfs());
      deq.pop_front();
      used[i] = false;
    } else if (s == c[i].b && se.find(c[i].a) == se.end()) {
      used[i] = true;
      deq.push_front(c[i].a);
      res = max(res, dfs());
      deq.pop_front();
      used[i] = false;
    }
  }
  return res;
}
int main() {
  while (scanf("%d", &n)) {
    if (!n)
      break;
    memset(used, 0, sizeof(used));
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &c[i].a, &c[i].b);
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
      used[i] = true;
      deq.push_back(c[i].a);
      deq.push_back(c[i].b);
      res = max(res, dfs());
      deq.pop_back();
      deq.pop_back();
      used[i] = false;
    }
    printf("%d\\n", res);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int result;
vector<int> strands[100];
bool used[100];

void rec(int k, int d, int ub) {
  if (result < d)
    result = d;
  used[k] = 1;
  for (int i = 0; i < strands[k].size(); i++)
    if (!used[strands[k][i]])
      ub--;
  ub++;
  if (result < ub)
    for (int i = 0; i < strands[k].size(); i++) {
      int j = strands[k][i];
      if (!used[j])
        rec(j, d + 1, ub);
    }
  used[k] = 0;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    for (int i = 0; i < 100; i++)
      strands[i].clear();
    fill(used, used + 100, false);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      strands[a].push_back(b);
      strands[b].push_back(a);
    }
    result = 0;
    for (int i = 0; i < 100; i++)
      rec(i, 1, n + 1);
    cout << result << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> strands[100];
bitset<100> used;

int rec(int k) {
  int r = 0;
  for (vector<int>::iterator i = strands[k].begin(); i != strands[k].end(); ++i)
    if (!used[*i]) {
      used[*i] = 1;
      r = max(r, rec(*i) + 1);
      used[*i] = 0;
    }
  return r;
}

int main() {
  for (;;) {
    cin >> n;
    if (!n)
      return 0;
    for (int i = 0; i < n; i++)
      strands[i].clear();
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      strands[a].push_back(b);
      strands[b].push_back(a);
    }
    int result = 0;
    for (int i = 0; i < n; i++)
      result = max(result, rec(i));
    cout << result << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> edge[100];
bool visit[100];
int solve(int n) {
  int mx = 1;
  visit[n] = 1;
  for (int i = 0; i < edge[n].size(); i++) {
    if (!visit[edge[n][i]]) {
      mx = max(mx, solve(edge[n][i]) + 1);
    }
  }
  visit[n] = 0;
  return mx;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < 100; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      a--;
      b--;
      edge[a].push_back(b);
      edge[b].push_back(a);
    }
    int mx = 0;
    for (int i = 0; i < 100; i++)
      mx = max(mx, solve(i));
    printf("%d\\n", mx);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<int> point, edge[100];
bool visit[100];
int solve(int n) {
  int mx = 0;
  for (int i = 0; i < edge[n].size(); i++) {
    if (!visit[edge[n][i]]) {
      visit[edge[n][i]] = true;
      mx = max(mx, solve(edge[n][i]) + 1);
      visit[edge[n][i]] = false;
    }
  }
  return mx;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      if (find(point.begin(), point.end(), a) == point.end())
        point.push_back(a);
      if (find(point.begin(), point.end(), b) == point.end())
        point.push_back(b);
      edge[a - 1].push_back(b - 1);
      edge[b - 1].push_back(a - 1);
    }
    int mx = 0;
    for (int i = 0; i < point.size(); i++) {
      visit[point[i]] = true;
      mx = max(mx, solve(point[i]) + 1);
      visit[point[i]] = false;
    }
    printf("%d\\n", mx);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()

#define INF INT_MAX

typedef unsigned int uint;

using namespace std;

vector<list<int>> graph;
set<int> nodes;
bool visited[100];

int solve(int start, int dist) {
  int res = -1;
  for (int to : graph[start]) {
    if (visited[to])
      continue;

    visited[to] = true;
    res = max(res, solve(to, dist + 1));
    visited[to] = false;
  }
  if (res == -1)
    return dist;
  return res;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    graph.clear();
    nodes.clear();
    graph.resize(100);
    memset(visited, 0, 100 * sizeof(bool));

    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      graph[a].push_back(b);
      graph[b].push_back(a);
      nodes.insert(a);
      nodes.insert(b);
    }

    int ans = 0;

    for (int node : nodes) {
      visited[node] = true;
      ans = max(solve(node, 0), ans);
      visited[node] = false;
    }

    cout << ans + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()

#define INF INT_MAX

typedef unsigned int uint;

using namespace std;

int table[100][100];
bool visited[100];

int solve(int start, int dist) {
  int res = -1;
  rep(i, 100) {
    if (visited[i] || table[start][i] == 0)
      continue;

    visited[i] = true;
    res = max(res, solve(i, dist + 1));
    visited[i] = false;
  }
  if (res == -1)
    return dist;
  return res;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    memset(table, 0, 100 * 100 * sizeof(int));
    memset(visited, 0, 100 * sizeof(bool));

    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      table[a][b] = table[b][a] = 1;
    }

    int ans = 0;

    rep(i, 100) {
      bool indep = true;
      rep(j, 100) {
        if (table[i][j] != 0) {
          indep = false;
          break;
        }
      }
      if (indep)
        continue;

      visited[i] = true;
      ans = max(solve(i, 0), ans);
      visited[i] = false;
    }

    cout << ans + 1 << endl;
  }
  return 0;
}