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

#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define sint(n) scanf("%d", &n)
#define sintt(n, m) scanf("%d%d", &n, &m)
#define sinttt(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define MAX 1000000000
#define end(a, b)                                                              \\
  if (a == b)                                                                  \\
    break;

using namespace std;

deque<int> kazu;

void saiki(int n, int am) {

  if (am == 0) {
    int mini = MAX;
    int flg = 1;
    rep(i, kazu.size()) {
      if (mini < kazu[i]) {
        flg = 0;
        break;
      }
      mini = kazu[i];
    }
    if (flg == 1) {
      rep(i, kazu.size() - 1) { printf("%d ", kazu[i]); }
      printf("%d\\n", kazu[kazu.size() - 1]);
    }
    return;
  }

  int mino = am;
  if (kazu.size() > 0) {
    mino = min(am, kazu[kazu.size() - 1]);
  }
  for (int i = mino - 1; i >= 0; i--) {
    kazu.push_back(i + 1);
    saiki(i + 1, am - (i + 1));
    kazu.pop_back();
  }
}

int main() {

  while (1) {
    int n;
    sint(n);
    end(n, 0);
    saiki(0, n);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define sint(n) scanf("%d", &n)
#define sintt(n, m) scanf("%d%d", &n, &m)
#define sinttt(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define MAX 1000000000
#define end(a, b)                                                              \\
  if (a == b)                                                                  \\
    break;

using namespace std;

deque<int> kazu;

void saiki(int n, int am) {

  if (am == 0) {
    int mini = MAX;
    int flg = 1;
    rep(i, kazu.size()) {
      if (mini < kazu[i]) {
        flg = 0;
        break;
      }
      mini = kazu[i];
    }
    if (flg == 1) {
      rep(i, kazu.size()) { printf("%d ", kazu[i]); }
      puts("");
    }
    return;
  }

  for (int i = am - 1; i >= 0; i--) {
    kazu.push_back(i + 1);
    saiki(i + 1, am - (i + 1));
    kazu.pop_back();
  }
}

int main() {

  while (1) {
    int n;
    sint(n);
    end(n, 0);
    saiki(0, n);
  }
}
Problem p00430 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

void solve(vector<int> vv) {
  map<vector<int>, bool> visited;
  queue<vector<int>> Q;
  Q.push(vv);
  while (Q.size()) {
    vector<int> v = Q.front();
    Q.pop();
    if (visited[v])
      continue;
    visited[v] = true;
    int n = v.size() - 1;
    REP(i, n) {
      if (i)
        cout << " ";
      cout << v[i];
    }
    cout << endl;
    REP(i, n) {
      if (v[i] > v[i + 1]) {
        v[i]--;
        for (int j = i; j < n; ++j) {
          if (v[j] > v[j + 1]) {
            vector<int> vv(v);
            vv[j + 1]++;
            if (j == n - 1)
              vv.push_back(0);
            Q.push(vv);
          }
        }
        v[i]++;
      }
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> v(2);
    v[0] = n;
    v[1] = 0;
    solve(v);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

void solve(vector<int> v) {
  int n = v.size();
  REP(i, n) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
  if (v[0] == 1)
    return;
  if (n > 1 && v[1] == v[0])
    return;
  v[0]--;
  REP(i, n - 1) {
    if (v[i + 1] < v[i]) {
      vector<int> vv(v);
      vv[i + 1]++;
      solve(vv);
    }
  }
  v.push_back(1);
  solve(v);
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> v(1, n);
    solve(v);
  }
}
Problem p00430 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

void func(int n, int m, vector<int> res) {

  if (n == 0) {
    for (int i = 0; i < res.size(); i++) {
      cout << res[i];
      if (i == res.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
    return;
  }

  for (int i = 0; i < m; i++) {
    if (m - i > n)
      continue;
    res.push_back(m - i);
    func(n - m + i, m - i, res);
    res.pop_back();
  }
}

int main(void) {

  int n;
  vector<int> res;

  while (cin >> n, n)
    func(n, n, res);

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int id = 1;
vector<int> ans[10000];

void func(vector<int> num) {

  sort(num.begin(), num.end(), greater<int>());

  for (int i = 0; i < num.size(); i++) {
    ans[id].push_back(num[i]);
  }
  id++;

  int p = num[0];
  sort(num.begin(), num.end());
  num.pop_back();

  for (int i = 1; i < p / 2 + p % 2; i++) {
    num.push_back(i);
    num.push_back(p - i);
    func(num);
    num.pop_back();
    num.pop_back();
  }
}

int main(void) {

  int n;
  vector<int> num;

  while (cin >> n, n) {
    num.clear();
    for (int i = 0; i < 10000; i++)
      ans[i].clear();

    for (int i = 0; i < n; i++)
      ans[0].push_back(1);
    num.push_back(n);
    func(num);

    int k = 1;
    for (int i = 0; i < id; i++) {
      for (int j = 0; j < id; j++) {
        if (ans[j].size() == k) {
          for (int l = 0; l < ans[j].size(); l++) {
            cout << ans[j][l];
            if (l == ans[j].size() - 1)
              cout << endl;
            else
              cout << " ";
          }
        }
      }
      k++;
    }
  }

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0507

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

class SquaresManager {
public:
  map<int, vector<string>> solutions;
  vector<string> solve(int numOfsquares);
};

vector<string> SquaresManager::solve(int numOfSquares) {
  try {
    return solutions.at(numOfSquares);
  } catch (out_of_range &e) {
    vector<string> solution;
    solution.push_back(to_string(numOfSquares));
    for (int numOfFirstBlocks = numOfSquares - 1; numOfFirstBlocks >= 1;
         numOfFirstBlocks--) {
      vector<string> setOfSubSolution = solve(numOfSquares - numOfFirstBlocks);
      for (auto pSolution = setOfSubSolution.begin();
           pSolution != setOfSubSolution.end(); ++pSolution) {
        if (numOfFirstBlocks >= atoi(&(*pSolution)[0])) {
          solution.push_back(to_string(numOfFirstBlocks) + " " + *pSolution);
        }
      }
    }
    solutions.insert({numOfSquares, solution});
    return solution;
  }
}

int main() {
  vector<int> setOfProblems;

  while (true) {
    int input;
    cin >> input;
    if (input == 0) {
      break;
    } else {
      setOfProblems.push_back(input);
    }
  }

  SquaresManager mng;
  for (auto itProblem = setOfProblems.begin(); itProblem != setOfProblems.end();
       ++itProblem) {
    vector<string> solution = mng.solve(*itProblem);
    for_each(solution.begin(), solution.end(),
             [](string arrangement) { cout << arrangement << endl; });
  }
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0507

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void square(vector<string> *, int);

int main() {
  vector<int> problems;
  int input;
  while (true) {
    cin >> input;
    if (input == 0) {
      break;
    } else {
      problems.push_back(input);
    }
  }

  for (auto it = problems.begin(); it != problems.end(); it++) {
    vector<string> squares;
    square(&squares, *it);
    for (auto its = squares.begin(); its != squares.end(); ++its) {
      cout << *its << endl;
    }
  }

  //	int wait;
  //	cin >> wait;
}

void square(vector<string> *outSquares, int numOfSquares) {
  (*outSquares).push_back(to_string(numOfSquares));
  for (int n = numOfSquares - 1; n >= 1; n--) {
    vector<string> result = {};
    square(&result, numOfSquares - n);
    for (auto it = result.begin(); it != result.end(); ++it) {
      if (n >= atoi(&(*it)[0])) {
        (*outSquares).push_back(to_string(n) + " " + (*it));
      }
    }
  }
}
Problem p00430 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void nanika(int use, vector<int> v, int m) {
  vector<int> V = v;
  V.push_back(m);
  if (V.size() > 1 && V[V.size() - 1] > V[V.size() - 2])
    return;

  if (use == 0) {
    for (int i = 0; i < V.size(); i++) {
      printf("%s%d", i > 0 ? " " : "", V[i]);
    }
    puts("");
    return;
  }

  for (int i = use; i > 0; i--) {
    if (use - i >= 0)
      nanika(use - i, V, i);
  }
  return;
}

int main(void) {
  int n;
  while (scanf("%d", &n) && n != 0) {
    vector<int> q;

    for (int i = n; i > 0; i--) {
      nanika(n - i, q, i);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int n;

void func(int now, int box[]) {
  int total = 0, i, flg = 0;

  for (i = 0; i < now; i++)
    total += box[i];
  if (total == n) {
    for (i = 0; i < now; i++) {
      if (flg == 1)
        printf(" ");
      printf("%d", box[i]);
      flg = 1;
    }
    puts("");
  } else {
    for (i = n - total; i >= 1; i--) {
      box[now] = i;
      while (box[now] > box[now - 1]) {
        box[now]--;
        i--;
        if (box[now] < 0) {
          box[now] = 0;
          break;
        }
      }
      func(now + 1, box);
    }
  }
}

int main(void) {
  while (scanf("%d", &n)) {
    int hako[50] = {0};
    if (n == 0)
      break;
    func(0, hako);
  }
  return 0;
}
Problem p00430 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int n, number[30];

void rec(int sum, int mins, int cnt) {
  if (sum == n) {
    for (int i = 0; i < cnt; i++) {
      if (i) {
        printf(" ");
      }

      printf("%d", number[i]);
    }

    printf("\\n");
    return;
  }

  for (int i = mins; i > 0; i--) {
    if (sum + i > n)
      continue;

    number[cnt] = i;

    rec(sum + i, i, cnt + 1);

    number[cnt] = 0;
  }
}

int main() {
  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    rec(0, n, 0);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int n, number[30];

void rec(int sum, int mins, int cnt) {
  if (sum == n) {
    for (int i = 0; i < cnt; i++) {
      if (i) {
        printf(" ");
      }

      printf("%d", number[i]);
    }

    printf("\\n");
    return;
  }

  for (int i = mins; i > 0; i++) {
    if (sum + i > n)
      continue;

    number[cnt] = i;

    rec(sum + i, i, cnt + 1);

    number[cnt] = 0;
  }
}

int main() {
  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    rec(0, n, 0);
  }

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

#include <stdio.h>

int na[32], n;

int narabi(int noko, int hai, int depth) {
  int i;
  ++depth;
  //	if(hai>n)return 0;
  //	printf("depth %d noko %d hai %d\\n",depth,noko,hai);
  if (noko == 0) {
    for (i = 0; i <= hai - 1; i++) {
      printf("%d ", na[i]);
    }
    printf("%d\\n", na[hai]);
    return 0;
  } else {
    for (i = noko; i > 0; i--) {
      if (na[hai] < i) {
        continue;
        na[hai + 1] = na[hai];
        narabi(noko - na[hai], hai + 1, depth);
      } else {
        na[hai + 1] = i;
        narabi(noko - i, hai + 1, depth);
      }
    }
  }
  return 0;
}

int main() {
  int i;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    for (i = n; i > 0; i--) {
      na[0] = i;
      narabi(n - i, 0, 0);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int na[30], n;

int narabi(int noko, int hai, int depth) {
  int i;
  ++depth;
  if (hai > n)
    return 0;
  //	printf("depth %d noko %d hai %d\\n",depth,noko,hai);
  if (noko == 1) {
    for (i = 0; i <= hai; i++) {
      printf("%d ", na[i]);
    }
    printf("1\\n");
    return 0;
  } else {
    if (na[hai] >= noko) {
      for (i = 0; i <= hai; i++) {
        printf("%d ", na[i]);
      }
      printf("%d\\n", noko);
    }

    for (i = noko - 1; i > 0; i--) {
      if (na[hai] < i) {
        na[hai + 1] = na[hai];
        narabi(noko - na[hai], hai + 1, depth);
      } else {
        na[hai + 1] = i;
        narabi(noko - i, hai + 1, depth);
      }
    }
  }
  return 0;
}

int main() {
  int i;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    printf("%d\\n", n);
    for (i = n - 1; i > 1; i--) {
      na[0] = i;
      narabi(n - i, 0, 0);
    }
    if (n == 1)
      continue;
    for (i = n; i > 1; i--) {
      printf("1 ");
    }
    printf("1\\n");
  }
  return 0;
}
Problem p00430 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int ans[32], last;
int mn(int a, int b) { return (a < b) ? a : b; }
void pintf() {
  for (int i = 0; i < last; printf("%d ", ans[i++]))
    ;
  printf("%d\\n", ans[last]);
}
void dfs(int val, int q) {
  val -= q;
  if (!val) {
    --last, pintf();
    return;
  }
  for (int i = mn(val, q); i >= 1; i--)
    ans[last++] = i, dfs(val, i);
  last--;
}
int main() {
  int N;
  while (1) {
    scanf("%d", &N);
    if (!N)
      return 0;
    last = 0;
    dfs(N << 1, N);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int ans[32], last;
int mn(int a, int b) { return (a < b) ? a : b; }
void pintf() {
  for (int i = 0; i < last; printf("%d ", ans[i++]))
    ;
  printf("%d\\n", ans[last]);
}
void dfs(int val, int q) {
  val -= q;
  if (!val) {
    --last, pintf();
    return;
  }
  for (int i = mn(val, q); i >= 1; i--)
    ans[last++] = i, dfs(val, i);
  last--;
}
int main() {
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  int N;
  while (1) {
    scanf("%d", &N);
    if (!N)
      return 0;
    dfs(N << 1, N);
  }
}
Problem p00430 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

using namespace std;

int main(void) {
  while (1) {
    vector<vector<int>> kek[31];

    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }

    for (int i = 1; i <= n; i++) {
      vector<int> hn;
      hn.push_back(i);
      kek[i].push_back(hn);

      for (int j = i - 1; j > 0; j--) {

        for (int k = 0; k < kek[i - j].size(); k++) {
          int flg = 0;

          for (int p = 0; p < kek[i - j][k].size(); p++) {
            if (kek[i - j][k][p] > j) {
              flg = 1;
              break;
            }
          }

          if (flg == 1) {
            continue;
          }

          vector<int> u;
          u.push_back(j);

          for (int p = 0; p < kek[i - j][k].size(); p++) {
            u.push_back(kek[i - j][k][p]);
          }
          kek[i].push_back(u);
        }
      }
    }

    for (int j = 0; j < kek[n].size(); j++) {
      printf("%d", kek[n][j][0]);
      for (int k = 1; k < kek[n][j].size(); k++) {
        printf(" %d", kek[n][j][k]);
      }
      puts("");
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

int main(void) {
  while (1) {
    vector<vector<int>> kek[31];

    int n;
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
      vector<int> hn;
      hn.push_back(i);
      kek[i].push_back(hn);

      for (int j = i - 1; j > 0; j--) {

        for (int k = 0; k < kek[i - j].size(); k++) {
          int flg = 0;

          for (int p = 0; p < kek[i - j][k].size(); p++) {
            if (kek[i - j][k][p] > j) {
              flg = 1;
              break;
            }
          }

          if (flg == 1) {
            continue;
          }

          vector<int> u;
          u.push_back(j);

          for (int p = 0; p < kek[i - j][k].size(); p++) {
            u.push_back(kek[i - j][k][p]);
          }
          kek[i].push_back(u);
        }
      }
    }

    for (int j = 0; j < kek[n].size(); j++) {
      for (int k = 0; k < kek[n][j].size(); k++) {
        printf("%d ", kek[n][j][k]);
      }
      puts("");
    }
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <time.h>

#define N 101

int find_length(int a, int cur_length);

int conn[N][N];
int used[N];

int main() {
  int i, j;
  int a, b;
  int n;
  int max_length, length;
  time_t start_time, t;

  while (1) {
    start_time = time(NULL);
    for (j = 0; j < N; j++) {
      for (i = 0; i < N; i++) {
        conn[j][i] = 0;
      }
    }

    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a, &b);
      conn[a][b] = 1;
      conn[b][a] = 1;
    }

    for (a = 0; a < N; a++) {
      used[a] = 0;
    }

    max_length = 0;
    for (a = 0; a < N; a++) {
      length = find_length(a, 1);
      if (length > max_length) {
        max_length = length;
      }
      if (time(NULL) - start_time > 1) {
        break;
      }
    }
    printf("%d\\n", max_length);
  }
}

int find_length(int a, int cur_length) {
  int b;
  int length, max_length;

  used[a] = 1;
  max_length = cur_length;
  for (b = 0; b < N; b++) {
    if (conn[a][b] == 1 && used[b] != 1) {
      length = find_length(b, cur_length + 1);
      if (length > max_length) {
        max_length = length;
      }
    }
  }
  used[a] = 0;

  return max_length;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <time.h>

#define N 101

int find_length(int a, int cur_length);

int conn[N][N];
int used[N];

int main() {
  int i, j;
  int a, b;
  int n;
  int max_length, length;
  time_t start_time, t;

  while (1) {
    start_time = time(NULL);
    for (j = 0; j < N; j++) {
      for (i = 0; i < N; i++) {
        conn[j][i] = 0;
      }
    }

    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a, &b);
      conn[a][b] = 1;
      conn[b][a] = 1;
    }

    for (a = 0; a < N; a++) {
      used[a] = 0;
    }

    max_length = 0;
    for (a = 0; a < N; a++) {
      length = find_length(a, 1);
      if (length > max_length) {
        max_length = length;
      }
      if (time(NULL) - start_time > 3) {
        break;
      }
    }
    printf("%d\\n", max_length);
  }
}

int find_length(int a, int cur_length) {
  int b;
  int length, max_length;

  used[a] = 1;
  max_length = cur_length;
  for (b = 0; b < N; b++) {
    if (conn[a][b] == 1 && used[b] != 1) {
      length = find_length(b, cur_length + 1);
      if (length > max_length) {
        max_length = length;
      }
    }
  }
  used[a] = 0;

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

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

vector<vector<int>> edge;
bool visited[128];
int n;
int depth = 0;

int solve(int k) {
  if (visited[k])
    return -1;
  visited[k] = true;
  int res = 0;
  for (int i = 0; i < edge[k].size(); i++) {
    res = max(res, solve(edge[k][i]) + 1);
  }
  visited[k] = false;
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < 128; i++)
      visited[i] = false;
    edge.clear();
    edge.resize(100);
    for (int i = 0; i < n; i++) {
      int a, b;
      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, solve(i) + 1);
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

int edge[128][128];
bool visited[128];
int n;
int depth = 0;

int solve(int k) {
  if (visited[k])
    return -1;
  visited[k] = true;
  int res = 0;
  for (int i = 0; i < 100; i++) {
    if (edge[k][i]) {
      res = max(res, solve(i) + 1);
    }
  }
  visited[k] = false;
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < 128 * 128; i++)
      edge[0][i] = 0;
    for (int i = 0; i < 128; i++)
      visited[i] = false;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      edge[a][b] = edge[b][a] = 1;
    }

    int res = 0;
    for (int i = 0; i < 100; i++) {
      res = max(res, solve(i) + 1);
    }
    cout << res << endl;
  }
}
Problem p00431 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool used[100];
vector<int> G[100];
int ma, mav;

void dfs(int v, int d) {
  if (ma < d) {
    ma = d;
    mav = v;
  }
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]]) {
      used[G[v][i]] = true;
      dfs(G[v][i], d + 1);
      used[G[v][i]] = false;
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < 100; i++) {
      G[i].clear();
      used[i] = false;
    }
    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);
    }
    ma = 0;
    for (int i = 0; i < 100; i++) {
      used[i] = true;
      dfs(i, 0);
      used[i] = false;
    }
    used[mav] = true;
    dfs(mav, 0);
    cout << ma + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int n;
int ma;
bool used[100];
vector<int> edge[100];

void dfs(int num, int cnt) {
  for (int i = 0; i < edge[num].size(); i++) {
    if (!used[edge[num][i]]) {
      used[edge[num][i]] = true;
      ma = max(ma, cnt + 1);
      dfs(edge[num][i], cnt + 1);
      used[edge[num][i]] = false;
    }
  }
}

int main() {
  while (cin >> n, n) {
    ma = 0;
    for (int i = 0; i < n; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      edge[a - 1].push_back(b - 1);
      edge[b - 1].push_back(a - 1);
      used[i] = 0;
    }
    for (int i = 0; i < n; i++)
      dfs(i, 0);
    cout << ma << endl;
  }
  return 0;
}