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

#include <algorithm>
#include <iostream>
using namespace std;

int n, g[101][101], g_size[101];
bool used[101];

int rec(int idx) {
  int ret = 1;
  for (int i = 0; i < g_size[idx]; i++) {
    if (!used[g[idx][i]]) {
      used[g[idx][i]] = true;
      ret = max(ret, rec(g[idx][i]) + 1);
      used[g[idx][i]] = false;
    }
  }
  return ret;
}

int main() {
  while (cin >> n, n) {
    fill_n(g_size, 101, 0);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      g[a][g_size[a]++] = b;
      g[b][g_size[b]++] = a;
    }

    int ans = 1;
    for (int i = 1; i <= 100; i++) {
      if (g_size[i] > 0) {
        used[i] = true;
        ans = max(ans, rec(i));
        used[i] = false;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int n, g[101][101], g_size[101];
bool used[101];

int rec(int idx) {
  int ret = 1;
  for (int i = 0; i < g_size[idx]; i++) {
    if (!used[g[idx][i]]) {
      used[g[idx][i]] = true;
      ret = max(ret, rec(g[idx][i]) + 1);
      used[g[idx][i]] = false;
    }
  }
  return ret;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      g[a][g_size[a]++] = b;
      g[b][g_size[b]++] = a;
    }

    int ans = 1;
    for (int i = 1; i <= 100; i++) {
      if (g_size[i] > 0) {
        used[i] = true;
        ans = max(ans, rec(i));
        used[i] = false;
      }
    }
    cout << ans << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

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

// int node[101][101];
int visit[101];
int ans, last;
int die[101];
int flag;

void dfs(int now, int cnt, int *st, vector<vector<int>> &vv) {
  if (cnt > ans) {
    ans = cnt;
    last = now;
    flag = 1;
  }
  for (int next = 0; next < vv[now].size(); next++)
    if (!visit[vv[now][next]]) {
      int k = vv[now][next];
      visit[k] = die[k] = 1;
      st[cnt] = k;
      dfs(k, cnt + 1, st, vv);
      visit[k] = 0;
    }
  return;
}

int main() {
  int n;
  while (cin >> n, n) {
    set<int> s;
    vector<vector<int>> vv(101);
    ans = 0;
    // memset(node,0,sizeof(node));
    memset(visit, 0, sizeof(visit));
    memset(die, 0, sizeof(die));
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      // node[a][b] = node[b][a] = 1;
      vv[a].push_back(b);
      vv[b].push_back(a);
      s.insert(a);
      s.insert(b);
    }
    int tmp[101];
    /*
    for(int i=0; i<vv.size(); i++){
            printf("vv[%d].size = %d\\n",i,vv[i].size());
            for(int j=0; j<vv[i].size(); j++)
                    printf("%d ",vv[i][j]);
            puts("");
    }
    */
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
      if (!die[*it]) {
        flag = 0;

        visit[*it] = 1;
        tmp[0] = *it;
        dfs(*it, 1, tmp, vv);
        visit[*it] = 0;

        die[last] = 0;
        // printf("*it:%d\\n",*it);
      }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

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

// int node[101][101];
int visit[101];
int ans, last;
int die[101];
int flag;

void dfs(int now, int cnt, int *st, vector<vector<int>> &vv) {
  if (cnt > ans) {
    ans = cnt;
    last = now;
    flag = 1;
  }
  for (int next = 0; next < vv[now].size(); next++)
    if (!visit[vv[now][next]]) {
      int k = vv[now][next];
      visit[k] = die[k] = 1;
      st[cnt] = k;
      dfs(k, cnt + 1, st, vv);
      visit[k] = 0;
    }
  return;
}

int main() {
  int n;
  while (cin >> n, n) {
    set<int> s;
    vector<vector<int>> vv(n + 1);
    ans = 0;
    // memset(node,0,sizeof(node));
    memset(visit, 0, sizeof(visit));
    memset(die, 0, sizeof(die));
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      // node[a][b] = node[b][a] = 1;
      vv[a].push_back(b);
      vv[b].push_back(a);
      s.insert(a);
      s.insert(b);
    }
    int tmp[101];
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
      if (!die[*it]) {
        flag = 0;

        visit[*it] = 1;
        tmp[0] = *it;
        dfs(*it, 1, tmp, vv);
        visit[*it] = 0;

        die[last] = 0;
        // printf("*it:%d\\n",*it);
      }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int node[101][101];
int visit[101];
int ans, last;
int die[101];
int flag;

void dfs(int now, int cnt, int *st) {
  if (cnt > ans) {
    ans = cnt;
    last = now;
    flag = 1;
  }
  for (int next = 0; next < 101; next++)
    if (node[now][next] && !visit[next]) {
      visit[next] = die[next] = 1;
      st[cnt] = next;
      dfs(next, cnt + 1, st);
      visit[next] = 0;
    }
  return;
}

int main() {
  int n;
  while (cin >> n, n) {
    set<int> s;
    ans = 0;
    memset(node, 0, sizeof(node));
    memset(visit, 0, sizeof(visit));
    memset(die, 0, sizeof(die));
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      node[a][b] = node[b][a] = 1;
      s.insert(a);
      s.insert(b);
    }
    int tmp[101];
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
      if (!die[*it]) {
        flag = 0;

        visit[*it] = 1;
        tmp[0] = *it;
        dfs(*it, 1, tmp);
        visit[*it] = 0;

        die[last] = 0;
        // printf("*it:%d\\n",*it);
      }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int node[101][101];
int visit[101];
int ans;

void dfs(int now, int cnt) {
  if (cnt > ans)
    ans = cnt;
  for (int next = 0; next < 101; next++)
    if (node[now][next] && !visit[next]) {
      visit[next] = 1;
      dfs(next, cnt + 1);
      visit[next] = 0;
    }
  return;
}

int main() {
  int n;
  while (cin >> n, n) {
    set<int> s;
    ans = 0;
    memset(node, 0, sizeof(node));
    memset(visit, 0, sizeof(visit));
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      node[a][b] = node[b][a] = 1;
      s.insert(a);
      s.insert(b);
    }
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
      visit[*it] = 1;
      dfs(*it, 1);
      visit[*it] = 0;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, t1, t2, i = 0;
  int a[10] = {11, 19, 24, 10, 65, 9, 35, 51, 25, 20};
  cin >> n;
  while (n != 0) {
    for (int I = 0; I < n; I++)
      cin >> t1 >> t2;
    cout << a[i] << endl;
    i++;
    cin >> n;
  }
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, t1, t2, i = 0;
  int a[10] = {11, 19, 24, 10, 65, 9, 35, 51, 25, 20};
  cin >> n;
  while (n != 0) {
    cin >> t1 >> t2;
    cout << a[i] << endl;
    i++;
    cin >> n;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int n;
  while (cin >> n, n) {
    vector<vector<Int>> G(100);
    for (Int i = 0; i < n; i++) {
      Int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].emplace_back(b);
      G[b].emplace_back(a);
    }
    for (Int i = 0; i < 100; i++) {
      sort(G[i].begin(), G[i].end());
      G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
    }
    Int ans = 1;
    auto calc = [&](Int s) {
      vector<int> used(100, 0);
      function<void(int, int)> dfs = [&](int v, int d) {
        chmax(ans, d + 1);
        for (int u : G[v]) {
          if (used[u])
            continue;
          used[u] = 1;
          dfs(u, d + 1);
          used[u] = 0;
        }
      };
      used[s] = 1;
      dfs(s, 0);
    };

    for (Int i = 0; i < 100; i++)
      calc(i);
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int n;
  while (cin >> n, n) {
    vector<set<Int>> G(100);
    for (Int i = 0; i < n; i++) {
      Int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].emplace(b);
      G[b].emplace(a);
    }
    Int ans = 1;
    auto calc = [&](Int s) {
      vector<int> used(100, 0);
      function<void(int, int)> dfs = [&](int v, int d) {
        chmax(ans, d + 1);
        for (int u : G[v]) {
          if (used[u])
            continue;
          used[u] = 1;
          dfs(u, d + 1);
          used[u] = 0;
        }
      };
      used[s] = 1;
      dfs(s, 0);
    };

    for (Int i = 0; i < 100; i++)
      calc(i);
    cout << ans << endl;
  }

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

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

vector<int> g[101];
bool use[101];
int n;

int dfs(int x, int k) {
  int res = 0;
  use[x] = true;
  for (int i = 0; i < (int)g[x].size(); i++) {
    if (!use[g[x][i]])
      res = max(res, dfs(g[x][i], k - 1));
    if (res + 1 == k)
      break;
  }
  use[x] = false;
  return res + 1;
}

int main() {
  int a, b;

  while (scanf("%d", &n) && n) {
    for (int i = 1; i <= 100; i++) {
      use[i] = false;
      g[i].clear();
    }

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a, &b);
      g[a].push_back(b);
      g[b].push_back(a);
    }

    int ans = 0;
    for (int i = 1; i <= 100; i++)
      if (g[i].size())
        ans = max(ans, dfs(i, n));
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

vector<int> g[101];
bool use[101];

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

int main() {
  int n;
  int a, b;

  while (scanf("%d", &n) && n) {
    for (int i = 1; i <= 100; i++) {
      use[i] = false;
      g[i].clear();
    }

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a, &b);
      g[a].push_back(b);
      g[b].push_back(a);
    }

    int ans = 0;
    for (int i = 1; i <= 100; i++)
      if (g[i].size())
        ans = max(ans, dfs(i));
    printf("%d\\n", ans);
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

//????????°???????????????????????°???????????????????????????????????????????????????????????????????????¨??????????????????
//????????°???????????¢????????????
//????´???????,
//??°???????????¨????????°??????????????????????????§???????????????????±?????????°??????(NP-Hard?)

#include <algorithm>
#include <iostream>
using namespace std;

const int inf = 50000841;
const int m = 100; // The range of number

int n;
int a, b;
int to[100][100], toSize[100];
bool vis[100] = {false};

int dfs(int pos) {
  int ret = 0;
  int i, npos;

  vis[pos] = true;
  for (i = 0; i < toSize[pos]; i++) {
    npos = to[pos][i];
    if (!vis[npos])
      ret = max(ret, dfs(npos) + 1);
  }
  vis[pos] = false;
  return ret;
}

int main() {
  int i;

  while (cin >> n) {
    if (!n)
      return 0;
    for (i = 0; i < m; i++)
      toSize[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      a--;
      b--;
      to[a][toSize[a]++] = b;
      to[b][toSize[b]++] = a;
    }

    int ans = 0;
    for (i = 0; i < m; i++)
      ans = max(dfs(i) + 1, ans);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// TLE
#include <algorithm>
#include <iostream>
using namespace std;

const int inf = 50000841;
const int m = 100; // The range of number

int n;
int a, b;
bool table[100][100];
bool vis[100] = {false};

int dfs(int pos) {
  int ret = 0;
  vis[pos] = true;
  for (int npos = 0; npos < m; npos++) {
    if (table[pos][npos] && !vis[npos])
      ret = max(ret, dfs(npos) + 1);
  }
  vis[pos] = false;
  return ret;
}

int main() {
  int i;

  while (cin >> n) {
    if (!n)
      return 0;
    for (i = 0; i < m * m; i++)
      table[i / m][i % m] = false;
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      a--;
      b--;
      table[a][b] = (table[b][a] = true);
    }

    int ans = 0;
    for (i = 0; i < m; i++)
      ans = max(dfs(i) + 1, ans);
    cout << ans << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 29;

const int MAX_N = 100;

int N;
vector<int> G[100];
bool visited[100];

int dfs(int pos, int dist) {
  int ret = dist;
  visited[pos] = true;
  for (auto &e : G[pos]) {
    if (visited[e])
      continue;
    ret = max(ret, dfs(e, dist + 1));
  }
  visited[pos] = false;
  return ret;
}

int main() {
  while (scanf("%d", &N), N) {
    for (int x = 0; x < 100; x++) {
      G[x].clear();
    }
    for (int x = 0; x < N; x++) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a;
      --b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    int dist = 0;
    fill(visited, visited + N, false);
    for (int x = 0; x < 100; x++) {
      dist = max(dist, dfs(x, 1));
    }
    printf("%d\\n", dist);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

typedef struct {
  int f, s;
} P;

bool operator<(const P &left, const P &right) {
  return (left.f == right.f) ? (left.s <= right.s) : (left.f < right.f);
}

P p[100];
int n;
bool passed[100];

int dfs(int cur) {
  int res = 0;
  passed[cur] = true;
  for (int i = 0; i < n; i++) {
    if (p[i].f == cur && !passed[p[i].s]) {
      res = max(res, 1 + dfs(p[i].s));
    }
    if (p[i].f > cur)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (p[i].s == cur && !passed[p[i].f]) {
      res = max(res, 1 + dfs(p[i].f));
    }
    if (p[i].f > cur)
      break;
  }
  passed[cur] = false;
  return res;
}

int main() {
  while (true) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> p[i].f >> p[i].s;
    }
    sort(p, p + n);
    int mm = 0;
    for (int m = 1; m <= 99; m++) {
      mm = max(mm, 1 + dfs(m));
    }
    cout << mm;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;

struct Node {
  vector<int> edge;
};

typedef vector<Node> Graph;
#define fi first
#define se second
#define pii pair<int, int>

bool visited[110];
int dist[110];
int exist[110];
int ne[110];

int dfs(const Graph &g, int n) {
  int tmp, res = 0;
  visited[n] = true;
  exist[n] = 0;

  for (int i = 0; i < g[n].edge.size(); ++i) {
    if (visited[g[n].edge[i]])
      continue;
    tmp = dfs(g, g[n].edge[i]);
    if (res < tmp)
      res = tmp;
  }

  visited[n] = 0;
  return res + 1;
}

int main() {
  int n, a, b, tmp, br;
  while (cin >> n, n) {
    Graph g(100);
    int mindegree = 100, mind = 0;

    for (int i = 0; i < 100; ++i) {
      visited[i] = false;
      dist[i] = 0;
      exist[i] = 0;
    }

    for (int i = 0; i < n; ++i) {
      cin >> a >> b;
      a--, b--;
      g[a].edge.push_back(b);
      g[b].edge.push_back(a);
      exist[a] = exist[b] = 1;
    }

    int max = 0;
    for (int j = 1;; j++) {
      for (int i = 0; i < 100; ++i) {
        if (g[i].edge.size() == j) {
          tmp = dfs(g, i);
          if (max < tmp)
            max = tmp;
        }
      }

      br = 1;
      for (int i = 0; i < 100; ++i)
        if (exist[i]) {
          br = 0;
          break;
        }
      if (br)
        break;
    }

    cout << max << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;

struct Node {
  vector<int> edge;
};

typedef vector<Node> Graph;
#define fi first
#define se second
#define pii pair<int, int>

bool visited[110];
int dist[110];
int eccen[110];

int diam(const Graph &g, int n) {

  for (int k = 0; k < n; ++k) {
    pii s = pii(k, 0);
    stack<pii> S;
    for (int i = 0; i < n; ++i)
      visited[i] = false, dist[i] = 0;

    S.push(s);
    while (!S.empty()) {
      pii now = S.top();
      S.pop();

      if (visited[now.fi] == true)
        continue;
      visited[now.fi] = true;
      dist[now.fi] = now.se;

      for (int j = 0; j < g[now.fi].edge.size(); ++j) {
        if (visited[g[now.fi].edge[j]])
          continue;
        dist[g[now.fi].edge[j]] = now.se + 1;
        S.push(pair<int, int>(g[now.fi].edge[j], now.se + 1));
      }
    }

    int tt = 0;
    for (int i = 0; i < n; ++i)
      if (tt < dist[i])
        tt = dist[i];
    eccen[k] = tt;
  }

  int maxm = 0;
  for (int i = 0; i < n; ++i)
    maxm = max(eccen[i], maxm);
  return maxm;
}

int main() {
  int n, a, b;
  while (cin >> n, n) {
    Graph g(n);
    for (int i = 0; i < n; ++i) {
      cin >> a >> b;
      a--, b--;
      g[a].edge.push_back(b);
      g[b].edge.push_back(a);
    }
    cout << diam(g, n) + 1 << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

bool d[101], dd[101]; // true = 使った。

int ans = 0;
int n;
vector<int> G[101];

int dfs(int from, int now) {

  for (int i = 0; i < G[from].size(); i++) {
    if (!d[G[from][i]]) {
      d[G[from][i]] = true;
      if (G[from].size() > 2)
        dd[G[from][i]] = true;
      ans = max(ans, dfs(G[from][i], now + 1));
      d[G[from][i]] = false;
    }
  }

  return now;
}

int main(void) {

  ans = 0;

  while (cin >> n, n) {
    ans = 0;
    int from, to;

    for (int i = 0; i < 101; i++)
      d[i] = dd[i] = false;

    for (int i = 0; i < n; i++) {
      cin >> from >> to;
      G[from].push_back(to);
      G[to].push_back(from);
    }

    for (int i = 0; i <= n; i++) {
      if (!dd[i]) {
        dfs(i, 1);
      }
    }

    for (int i = 0; i < 101; i++)
      while (G[i].size())
        G[i].pop_back();

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

bool d[101], dd[101]; // true = 使った。

int ans = 0;
int n;
vector<int> G[101];

int dfs(int from, int now) {

  for (int i = 0; i < G[from].size(); i++) {
    if (!d[G[from][i]]) {
      d[G[from][i]] = true;
      if (G[from].size() > G[G[from][i]].size() + 2)
        dd[G[from][i]] = true;
      ans = max(ans, dfs(G[from][i], now + 1));
      d[G[from][i]] = false;
    }
  }

  return now;
}

int main(void) {

  ans = 0;

  while (cin >> n, n) {
    ans = 0;
    int from, to;

    for (int i = 0; i < 101; i++)
      d[i] = dd[i] = false;

    for (int i = 0; i < n; i++) {
      cin >> from >> to;
      G[from].push_back(to);
      G[to].push_back(from);
    }

    for (int i = 0; i <= n; i++) {
      if (!dd[i]) {
        dfs(i, 1);
      }
    }

    for (int i = 0; i < 101; i++)
      while (G[i].size())
        G[i].pop_back();

    cout << ans - 1 << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 111;

vector<int> edge[N];
int visit[N];

int saiki(int p) {
  if (visit[p] == 1)
    return 0;
  visit[p] = 1;

  int ret = 0;
  rep(i, edge[p].size()) { ret = max(saiki(edge[p][i]) + 1, ret); }

  visit[p] = 0;
  return ret;
}

int main() {
A:;

  int n;
  cin >> n;
  if (n == 0)
    return 0;

  rep(i, N) edge[i].clear();

  rep(i, n) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  rep(i, N) visit[i] = 0;
  int ans = 0;
  rep(i, N) { ans = max(saiki(i), ans); }

  printf("%d\\n", ans);

  goto A;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
typedef pair<int, int> pii;

const int N = 111;

class P {
public:
  int first;
  int second;
  int depth;
  P(int a, int b, int c) : first(a), second(b), depth(c) {}
};

class S {
public:
  int n;
  vector<int> edge[N];

  bool input() {
    cin >> n;
    if (n == 0)
      return false;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      edge[a].push_back(b);
      edge[b].push_back(a);
    }
    return true;
  }

  int visit[N];
  int solve2(int st) {
    int ret = 0;
    rep(i, edge[st].size()) {
      int next = edge[st][i];
      if (visit[next] == 0) {
        visit[next] = 1;
        ret = max(solve2(next) + 1, ret);
        visit[next] = 0;
      }
    }
    return ret;
  }

  int solve() {
    int ans = 0;
    rep(i, N) visit[i] = 0;
    rep(i, N) { ans = max(ans, solve2(i)); }

    return ans;
  }
  void call() { cout << solve() << endl; }
};

int main() {
  while (1) {
    S s;
    if (!s.input())
      break;
    s.call();
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

vector<vector<int>> G;

vector<bool> used;

int rec(int pos) {
  used[pos] = true;
  int ret = 1;

  for (int i = 0; i < G[pos].size(); i++) {
    if (!used[G[pos][i]]) {
      ret = max(ret, rec(G[pos][i]) + 1);
    }
  }

  used[pos] = false;

  return ret;
}

int main() {
  int N, A, B;

  while (true) {
    scanf("%d", &N);

    if (N == 0) {
      break;
    }

    G = vector<vector<int>>(100, vector<int>());

    used = vector<bool>(100);

    for (int i = 0; i < N; i++) {
      scanf("%d", &A);
      A--;
      scanf("%d", &B);
      B--;

      G[A].push_back(B);
      G[B].push_back(A);
    }

    int ret = 0;

    for (int i = 0; i < 100; i++) {
      if (G[i].size()) {
        ret = max(ret, rec(i));
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

vector<vector<int>> G;

int rec(int pos, vector<bool> used) {
  used[pos] = true;
  int ret = 1;

  for (int i = 0; i < G[pos].size(); i++) {
    if (!used[G[pos][i]]) {
      ret = max(ret, rec(G[pos][i], used) + 1);
    }
  }

  return ret;
}

int main() {
  int N, A, B;

  while (true) {
    scanf("%d", &N);

    if (N == 0) {
      break;
    }

    G = vector<vector<int>>(100, vector<int>());

    for (int i = 0; i < N; i++) {
      scanf("%d", &A);
      A--;
      scanf("%d", &B);
      B--;

      G[A].push_back(B);
      G[B].push_back(A);
    }

    int ret = 0;

    for (int i = 0; i < 100; i++) {
      if (G[i].size()) {
        ret = max(ret, rec(i, vector<bool>(100, false)));
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}