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

#include <iostream>
#include <vector>

constexpr int MAX = 100;

bool visited[MAX];

int dfs(int v, const std::vector<std::vector<int>> &G) {
  visited[v] = 1;
  int res = 0;
  for (int to : G[v]) {
    if (!visited[to]) {
      res = std::max(res, dfs(to, G) + 1);
    }
  }
  visited[v] = 0;
  return res;
}

int main() {
  int N;
  while (1) {
    std::cin >> N;
    if (N == 0) {
      break;
    }
    std::vector<std::vector<int>> G(MAX);
    for (int i = 0; i < N; i++) {
      int a, b;
      std::cin >> a >> b;
      a--;
      b--;
      G[a].emplace_back(b);
      G[b].emplace_back(a);
    }

    int res = 0;
    for (int i = 0; i < MAX; i++) {
      std::fill(visited, visited + MAX, 0);
      res = std::max(res, dfs(i, G) + 1);
    }

    std::cout << res << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

constexpr int MAX = 100;

bool visited[MAX];

int dfs(int v, const std::vector<std::vector<int>> &G) {
  int res = 0;
  for (int to : G[v]) {
    if (!visited[to]) {
      visited[to] = 1;
      res = std::max(res, dfs(to, G) + 1);
      visited[to] = 0;
    }
  }
  return res;
}

int main() {
  int N;
  while (1) {
    std::cin >> N;
    if (N == 0) {
      break;
    }
    std::vector<std::vector<int>> G(MAX);
    for (int i = 0; i < N; i++) {
      int a, b;
      std::cin >> a >> b;
      a--;
      b--;
      G[a].emplace_back(b);
      G[b].emplace_back(a);
    }

    int res = 0;
    for (int i = 0; i < N; i++) {
      std::fill(visited, visited + MAX, 0);
      res = std::max(res, dfs(i, G));
    }

    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
vvi in;
vi visit;
int out = 1;
void dfs(int a, int c) {
  out = max(out, c + 1);
  rep(i, in[a].size()) {
    int t = in[a][i];
    if (!visit[t]) {
      visit[t] = true;
      dfs(t, c + 1);
      visit[t] = false;
    }
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    in = vvi(101);
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      in[a].pb(b);
      in[b].pb(a);
    }
    int mi = inf;
    rep(i, 101) if (in[i].size()) {
      int t = in[i].size();
      mi = min(mi, t);
    }
    out = 0;
    rep(i, 101) { // if(mi==in[i].size()){
      visit = vi(101);
      visit[i] = true;
      dfs(i, 0);
    }
    cout << out << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
vvi in;
vi d;
void dfs(int a, int c) {
  d[a] = c;
  rep(i, in[a].size()) {
    int t = in[a][i];
    if (d[t] == inf)
      dfs(t, c + 1);
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    in = vvi(n);
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      in[a].pb(b);
      in[b].pb(a);
    }
    int out = 0;
    rep(i, n) {
      d = vi(n, inf);
      dfs(i, 0);
      rep(j, n) if (d[j] != inf) out = max(out, d[j]);
      //			rep(j,n)cout<<" "<<d[j];cout<<endl;
    }
    cout << out + 1 << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#pragma warning(disable : 4996)
using namespace std;
int N, A, B, v[100], r[100], G[100][199];
int rec(int pos) {
  int ret = 0;
  v[pos] = 1;
  for (int i = 0; i < r[pos]; i++) {
    if (!v[G[pos][i]])
      ret = max(ret, rec(G[pos][i]));
  }
  v[pos] = 0;
  return ret + 1;
}
int main() {
  while (scanf("%d", &N), N) {
    for (int i = 0; i < 100; i++)
      r[i] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &A, &B), A--, B--;
      G[A][r[A]++] = B, G[B][r[B]++] = A;
    }
    int ret = 0;
    for (int i = 0; i < 100; i++)
      ret = max(ret, rec(i));
    printf("%d\\n", ret);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int N, A, B, v[100], r[100], G[100][199];
int rec(int pos) {
  v[pos] = 1;
  int ret = 0;
  for (int i = 0; i < r[pos]; i++) {
    if (!v[G[pos][i]])
      ret = max(ret, rec(G[pos][i]));
  }
  v[pos] = 0;
  return ret + 1;
}
int main() {
  while (scanf("%d%d", &N), N) {
    for (int i = 0; i < 100; i++)
      r[i] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &A, &B), A--, B--;
      G[A][r[A]++] = B;
      G[B][r[B]++] = A;
    }
    int ret = 0;
    for (int i = 0; i < 100; i++)
      ret = max(ret, rec(i));
    printf("%d\\n", ret);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> G[101];
bool passed[101];
int maxPath = 0;
int maxNode;

void dfs(int s, int depth) {
  maxPath = max(maxPath, depth);
  if (maxPath == maxNode)
    return;
  for (int i = 0; i < G[s].size(); i++) {
    int to = G[s][i];
    if (!passed[to]) {
      passed[to] = true;
      dfs(to, depth + 1);
      passed[to] = false;
      if (maxPath == maxNode)
        return;
    }
  }
}

int main() {
  int n;
  while (scanf("%d", &n) && n != 0) {
    maxPath = 0;
    memset(passed, 0, sizeof(passed));
    for (int i = 0; i < 101; i++)
      G[i].clear();
    set<int> s;
    for (int i = 0; i < n; i++) {
      int a, b;
      // cin>>a>>b;
      scanf("%d %d", &a, &b);
      a--;
      b--;
      s.insert(a);
      s.insert(b);
      G[a].push_back(b);
      G[b].push_back(a);
    }
    maxNode = s.size();
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
      passed[*it] = true;
      dfs(*it, 1);
      passed[*it] = false;
      if (maxPath == maxNode)
        break;
    }
    printf("%d\\n", maxPath);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool d[101][101];
vector<int> G[101];
bool passed[101];
int maxPath = 0;
int maxNode;
bool isAppeared[101];

void dfs(int s, int depth) {
  maxPath = max(maxPath, depth);
  if (maxPath == maxNode)
    return;
  for (int i = 0; i < G[s].size(); i++) {
    if (!passed[G[s][i]]) {
      passed[G[s][i]] = true;
      dfs(G[s][i], depth + 1);
      passed[G[s][i]] = false;
      if (maxPath == maxNode)
        return;
    }
  }
}

int main() {
  int n;
  while (cin >> n && n != 0) {
    maxPath = 0;
    fill(passed, passed + 101, false);
    fill(isAppeared, isAppeared + 101, false);
    for (int i = 0; i < 101; i++)
      G[i].clear();
    set<int> s;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      s.insert(a);
      s.insert(b);
      isAppeared[a] = true;
      isAppeared[b] = true;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    maxNode = s.size();
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
      if (!isAppeared[*it])
        continue;
      passed[*it] = true;
      dfs(*it, 1);
      if (maxPath == maxNode)
        break;
      passed[*it] = false;
    }
    cout << maxPath << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = k; i < j; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int N = 110;

vector<vector<int>> d(N, vector<int>());
int ans = 2;

void dfs(int now, bool visited[], int sum) {
  sum++;
  int dsize = d[now].size();
  REP(i, dsize) {
    int next = d[now][i];
    if (!visited[next]) {
      visited[next] = true;
      dfs(next, visited, sum);
      visited[next] = false;
    }
  }
  ans = max(ans, sum);
}

int main() {
  int n;
  vector<int> v;
  while (cin >> n && n) {
    ans = 2;
    REP(i, N) d[i].clear();
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      d[a].push_back(b);
      d[b].push_back(a);
      if (find(v.begin(), v.end(), a) == v.end())
        v.push_back(a);
      if (find(v.begin(), v.end(), b) == v.end())
        v.push_back(b);
    }
    int vsize = v.size();
    REP(i, vsize) {
      bool visited[N];
      memset(visited, 0, sizeof(visited));
      visited[v[i]] = true;
      dfs(v[i], visited, 0);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = k; i < j; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int N = 110;

int main() {
  int n;
  while (cin >> n && n) {
    vector<vector<int>> d(N, vector<int>());
    queue<vector<int>> q;
    bool findFlg[N] = {false};
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      d[a].push_back(b);
      d[b].push_back(a);
      if (!findFlg[a]) {
        vector<int> v;
        v.push_back(a);
        q.push(v);
      }
      if (!findFlg[b]) {
        vector<int> v;
        v.push_back(b);
        q.push(v);
      }
      findFlg[a] = true;
      findFlg[b] = true;
    }

    int ans = -1;
    while (!q.empty()) {
      vector<int> nowv = q.front();
      q.pop();
      int now = nowv.back();

      REP(i, d[now].size()) {
        int next = d[now][i];
        if (find(nowv.begin(), nowv.end(), next) == nowv.end()) {
          vector<int> nextv = nowv;
          nextv.push_back(next);
          q.push(nextv);
        } else {
          int s = nowv.size();
          ans = max(ans, s);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> d[100];
bool visited[100];
int dist;
int max_dist;
void dfs(int s) {
  dist++;
  if (dist > max_dist)
    max_dist = dist;
  visited[s] = true;
  for (int i = 0; i < d[s].size(); i++) {
    int t = d[s][i];
    if (visited[t] == false) {
      dfs(t);
      dist--;
      visited[t] = false;
    }
  }
  visited[s] = false;
  return;
}
int main() {
  int N, a, b;
  for (;;) {
    scanf("%d", &N);
    if (N == 0)
      break;
    max_dist = 0;
    for (int i = 0; i < 100; i++)
      d[i].clear();
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &a, &b);
      a--;
      b--;
      d[a].push_back(b);
      d[b].push_back(a);
    }
    for (int i = 0; i < 100; i++) {
      fill(visited, visited + 100, false);
      dist = 0;
      dfs(i);
    }
    printf("%d\\n", max_dist);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool d[100][100];
bool visited[100];
int dist;
int max_dist;
void dfs(int s) {
  dist++;
  if (dist > max_dist)
    max_dist = dist;
  visited[s] = true;
  bool flag = true;
  for (int i = 0; i < 100; i++) {
    if (d[s][i] == true && visited[i] == false) {
      flag = false;
      dfs(i);
      dist--;
      visited[i] = false;
    }
  }
  if (flag)
    return;
}
int main() {
  int N, a, b;
  for (;;) {
    scanf("%d", &N);
    if (N == 0)
      break;
    max_dist = 0;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        d[i][j] = false;
      }
    }
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &a, &b);
      a--;
      b--;
      d[a][b] = true;
      d[b][a] = true;
    }
    for (int i = 0; i < 100; i++) {
      fill(visited, visited + 100, false);
      dist = 0;
      dfs(i);
    }
    printf("%d\\n", max_dist);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int N;
vector<int> E[101];
bool V[101];
int ans;

void dfs(int v, int lvl) {
  if (V[v]) {
    ans = max(ans, lvl);
  } else {
    V[v] = true;
    for (int i = 0; i < E[v].size(); i++) {
      dfs(E[v][i], lvl + 1);
    }
    V[v] = false;
  }
}

int main(void) {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    REP(i, 0, N) {
      int a, b;
      cin >> a >> b;
      E[a].push_back(b);
      E[b].push_back(a);
    }

    ans = 0;
    REP(i, 1, 101) { dfs(i, 0); }

    cout << ans << endl;

    REP(i, 1, 101) { E[i].clear(); }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int N;
vector<pair<int, int>> E[101];
bool V[100];
int C[101];
int x;
int ans;

void dfs(int v, int lvl) {
  ans = max(ans, lvl);

  int nlvl = C[v]++ == 0 ? lvl + 1 : lvl;

  for (int i = 0; i < E[v].size(); i++) {
    if (!V[E[v][i].second]) {
      V[E[v][i].second] = true;
      dfs(E[v][i].first, nlvl);
      V[E[v][i].second] = false;
    }
  }

  C[v]--;
}

int main(void) {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    REP(i, 0, N) {
      int a, b;
      cin >> a >> b;
      V[i] = false;
      E[a].push_back(pair<int, int>(b, i));
      E[b].push_back(pair<int, int>(a, i));
      x = max(x, max(a, b));
    }
    REP(i, 1, N + 1) { C[i] = 0; }

    ans = 0;
    REP(i, 1, x + 1) { dfs(i, 0); }

    cout << ans << endl;

    REP(i, 1, 101) { E[i].clear(); }
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int ans;

void Dfs(vector<int> way[100], bool used[100], int n, int d) {
  // printf("%d,%d\\n",n,d);
  if (used[n])
    return;
  used[n] = true;
  ans = max(ans, d);
  for (int i = 0; i < way[n].size(); i++) {
    Dfs(way, used, way[n][i], d + 1);
  }
  used[n] = false;
}

int main() {
  int N, A, B;
  while (cin >> N, N) {
    ans = 0;
    vector<int> way[100];
    for (int i = 0; i < N; i++) {
      cin >> A >> B;
      A--;
      B--;
      way[A].push_back(B);
      way[B].push_back(A);
    }
    bool used[100] = {};
    for (int i = 0; i < 100; i++) {
      Dfs(way, used, i, 1);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> way[100];
int ans;

void Dfs(bool used[100], int n, int d) {
  used[n] = true;
  ans = max(ans, d);
  for (int i = 0; i < way[n].size(); i++) {
    if (used[way[n][i]])
      continue;
    Dfs(used, way[n][i], d + 1);
  }
  used[n] = false;
}

int main() {
  int N, A, B;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> A >> B;
      A--;
      B--;
      way[A].push_back(B);
      way[B].push_back(A);
    }
    bool used[100] = {};
    for (int i = 0; i < N; i++) {
      Dfs(used, i, 1);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j) for (int i = 0; i < (j); i++)
typedef vector<int> vi;
vi G[100];
bool memo[101];
int func(int s, int n) {
  int ret = n;
  if (memo[s])
    return ret;
  memo[s] = true;
  rep(i, G[s].size()) { ret = max(ret, func(G[s][i], n + 1)); }
  memo[s] = false;
  return ret;
}
int main() {
  int n;
  while (cin >> n, n) {
    rep(i, 100) G[i].clear();
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    int ans = -1;
    rep(i, 100) {
      memset(memo, 0, sizeof(memo));
      ans = max(ans, func(i, 0));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (j); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
vvi G = vvi(100, vi(100, -1));
bool memo[101];
int func(int s, int n) {
  memo[s] = true;
  int ret = n;
  rep(i, G[s].size()) {
    if (memo[G[s][i]])
      continue;
    ret = max(ret, func(G[s][i], n + 1));
  }
  memo[s] = false;
  return ret;
}
int main() {
  int n;
  while (cin >> n, n) {
    rep(i, G.size()) rep(j, G[i].size()) G[i][j] = -1;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    int ans = -1;
    rep(i, G.size()) {
      memset(memo, 0, sizeof(memo));
      ans = max(ans, func(i, 0));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

set<int> vertex;
vector<vector<int>> edge;

int dfs(int v, bitset<101> visited, int depth) {
  int result = depth;
  visited.set(v);
  for (vector<int>::iterator i = edge[v].begin(); i != edge[v].end(); i++) {
    if (visited.test(*i))
      continue;
    result = max(result, dfs(*i, visited, depth + 1));
  }
  return result;
}

int solve() {
  int result = 0;
  for (set<int>::iterator i = vertex.begin(); i != vertex.end(); i++) {
    result = max(result, dfs(*i, bitset<101>(0), 1));
  }
  return result;
}

int main() {
  int n;
  while (cin >> n, n) {
    edge = vector<vector<int>>(101);
    for (int i = 0; i < n; i++) {
      int u, v;
      cin >> u >> v;
      vertex.insert(--u);
      vertex.insert(--v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    }
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

set<int> vertex;
vector<vector<int>> edge;

int dfs(int v, bitset<101> visited) {
  visited.set(v);
  int result = visited.count();
  for (vector<int>::iterator i = edge[v].begin(); i != edge[v].end(); i++) {
    if (visited.test(*i))
      continue;
    result = max(result, dfs(*i, visited));
  }
  return result;
}

int main() {
  int n;
  while (cin >> n, n) {
    edge = vector<vector<int>>(101);
    for (int i = 0; i < n; i++) {
      int u, v;
      cin >> u >> v;
      vertex.insert(--u);
      vertex.insert(--v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    }
    int result = 0;
    for (set<int>::iterator j = vertex.begin(); j != vertex.end(); j++) {
      result = max(result, dfs(*j, bitset<101>(0)));
    }
    cout << result << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dfs(vector<vector<int>> &v, int now, vector<bool> &used, int count) {
  if (used[now])
    return count;
  count++;

  used[now] = true;
  int ret = count;
  for (auto i : v[now]) {
    ret = max(ret, dfs(v, i, used, count));
  }
  used[now] = false;

  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<int>> v(101);
    for (int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }

    int ans = 0;
    vector<bool> used(101, false);
    for (int i = 1; i <= 100; i++) {
      ans = max(ans, dfs(v, i, used, 0));
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int dfs(vector<vector<int>> &v, int now, vector<int> used, int count) {
  if (used[now])
    return count;
  count++;

  used[now] = true;
  int ret = 0;
  for (auto i : v[now]) {
    ret = max(ret, dfs(v, i, used, count));
  }

  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<int>> v(101);
    for (int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }

    int ans = 0;
    for (int i = 1; i <= 100; i++) {
      ans = max(ans, dfs(v, i, vector<int>(101, false), 0));
    }
    cout << ans << endl;
  }

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

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

int n;
vector<int> G[100];
bool D[100];
int r, k;

void solve(int m) {
  D[m] = 1;
  r = max(r, ++k);
  for (vector<int>::iterator it = G[m].begin(); it != G[m].end(); it++) {
    if (!D[*it]) {
      solve(*it);
    }
  }
  --k;
  D[m] = 0;
}

int main() {
  for (int i = 0; i < 100; i++)
    G[i].reserve(100);
  while (scanf("%d", &n), n) {
    r = 0;
    for (int i = 0; i < 100; i++)
      G[i].clear();
    while (n--) {
      int a, b;
      scanf("%d%d", &a, &b);
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    for (int i = 0; i < 100; i++) {
      solve(i);
    }
    printf("%d\\n", r);
  }
}

Error: TLE

Fixed submission:

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

int n;
vector<short> G[100];
bool D[100];
int r, k;

void solve(int m) {
  D[m] = 1;
  r = max(r, ++k);
  for (vector<short>::iterator it = G[m].begin(); it != G[m].end(); it++) {
    if (!D[*it]) {
      solve(*it);
    }
  }
  --k;
  D[m] = 0;
}

int main() {
  while (scanf("%d", &n), n) {
    r = 0;
    for (int i = 0; i < 100; i++)
      G[i].clear();
    while (n--) {
      int a, b;
      scanf("%d%d", &a, &b);
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    for (int i = 0; i < 100; i++) {
      solve(i);
    }
    printf("%d\\n", r);
  }
}