Problem p00431 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0508>
?????????============================================================
?????????????????°?????????????´?????????????.????????°?????£??´??°??????????????????,?´?????????\\??????.
?´?????????????????????°????????°????????° a, b ??????????????????.?????????,[a,
b] ??¨?¨???°??????.
?????°????´??????????,??????????´???¨??????????´????????????°????????????????????°???????????´???,??????????????°?????¨????????§,
?????????????´?????????????????????¨?????§??????,??????????????§????????????????????¨?????¶?????¨?????????.
????????°,[1, 3] ??¨ [3, 4] ??¨???????´?????????? [1, 3, 4]
??¨??????????????§??????.
?´???¨??????????????????????????´??°??????????????????????????°?????¨????????§?????????????????¨?????§??????????????¨??????.

????????°,??? [1, 3, 4] ??¨?´? [5, 1] ????????? [5, 1, 3, 4] ?????§???,
??? [1, 3, 4] ??¨??? [2, 3, 5] ?????????,
????????§??????????????????????????¢?????????.
??? [1, 3, 4] ??¨??? [4, 6, 1] ?????????, ????????¢?????§??????.

????????????????§?????????¢?????§?????????, ????????????????????¨??§,
????????°????????????????????°???????????????????????????????????£????????°????´?????????¨?????¶?????¨?????????.
????????°,??? [1, 3, 4] ??¨???[2, 3, 5]
????????§??????,????????§??????????????????????????¢??????, [1, 3, 5],[2, 3, 4]
??¨?????????????????????,
??? [1, 3, 4] ??¨??? [4, 6, 1] ????????§???????????????,
[1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]???????????????????????????.

???????????????????????????,??????????????°????????°???????????¨????????????.
????????????????????°????´?????????????,??????????????????????????????????????????????????¨1?????\\????????????????????¢?????§??????.
????????????????????§???????????????????±???????????????°?????????????????????.

??\\???????????? ??????????????????????´?????????°??§????????£??´??° 1 ??? n ???
100 ??????????????????,
?????\\??? n ????????????????????????, ????????§???????????????
2 ????????´??° a, b ?????????????????? 1 ??? a < b ??? 100 ??¨?????£?????????.
????????? 2 ????????´??°??? 1 ???????´???????????????´??°?????¨??????.
=================================================================
??\\???=============================================================
??\\???????????°????????????????????????????????????n ??? 0
?????¨?????\\????????????????????????????????????????????°??? 10
????¶?????????????
================================================================
??????=============================================================
???????????????????????¨???????????§??????????????????????????????????????????
================================================================
?§£??¬=============================================================
================================================================
*/

ll ans;
void dfs(ll n, ll dep, vector<vector<ll>> &G, vector<int> &used) {
  bool f = false;
  for (auto next : G[n]) {
    if (used[next] == 0) {
      f = true;
      used[next] = 1;
      dfs(next, dep + 1, G, used);
      used[next] = 0;
    }
  }
  if (!f) {
    ans = max(ans, dep);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while (cin >> n, n) {
    ans = 0;
    vector<vector<ll>> G(101);
    for (int i = 0; i < n; i++) {
      ll x, y;
      cin >> x >> y;
      x--;
      y--;
      G[x].push_back(y);
      G[y].push_back(x);
    }

    vector<int> used(101, 0);
    for (int i = 0; i < 101; i++) {
      used[i] = 1;
      dfs(i, 1, G, used);
      used[i] = 0;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0508>
?????????============================================================
?????????????????°?????????????´?????????????.????????°?????£??´??°??????????????????,?´?????????\\??????.
?´?????????????????????°????????°????????° a, b ??????????????????.?????????,[a,
b] ??¨?¨???°??????.
?????°????´??????????,??????????´???¨??????????´????????????°????????????????????°???????????´???,??????????????°?????¨????????§,
?????????????´?????????????????????¨?????§??????,??????????????§????????????????????¨?????¶?????¨?????????.
????????°,[1, 3] ??¨ [3, 4] ??¨???????´?????????? [1, 3, 4]
??¨??????????????§??????.
?´???¨??????????????????????????´??°??????????????????????????°?????¨????????§?????????????????¨?????§??????????????¨??????.

????????°,??? [1, 3, 4] ??¨?´? [5, 1] ????????? [5, 1, 3, 4] ?????§???,
??? [1, 3, 4] ??¨??? [2, 3, 5] ?????????,
????????§??????????????????????????¢?????????.
??? [1, 3, 4] ??¨??? [4, 6, 1] ?????????, ????????¢?????§??????.

????????????????§?????????¢?????§?????????, ????????????????????¨??§,
????????°????????????????????°???????????????????????????????????£????????°????´?????????¨?????¶?????¨?????????.
????????°,??? [1, 3, 4] ??¨???[2, 3, 5]
????????§??????,????????§??????????????????????????¢??????, [1, 3, 5],[2, 3, 4]
??¨?????????????????????,
??? [1, 3, 4] ??¨??? [4, 6, 1] ????????§???????????????,
[1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]???????????????????????????.

???????????????????????????,??????????????°????????°???????????¨????????????.
????????????????????°????´?????????????,??????????????????????????????????????????????????¨1?????\\????????????????????¢?????§??????.
????????????????????§???????????????????±???????????????°?????????????????????.

??\\???????????? ??????????????????????´?????????°??§????????£??´??° 1 ??? n ???
100 ??????????????????,
?????\\??? n ????????????????????????, ????????§???????????????
2 ????????´??° a, b ?????????????????? 1 ??? a < b ??? 100 ??¨?????£?????????.
????????? 2 ????????´??°??? 1 ???????´???????????????´??°?????¨??????.
=================================================================
??\\???=============================================================
??\\???????????°????????????????????????????????????n ??? 0
?????¨?????\\????????????????????????????????????????????°??? 10
????¶?????????????
================================================================
??????=============================================================
???????????????????????¨???????????§??????????????????????????????????????????
================================================================
?§£??¬=============================================================
================================================================
*/

ll ans;
void dfs(ll n, ll dep, vector<vector<ll>> &G, vector<int> &used) {
  bool f = false;
  for (auto next : G[n]) {
    if (used[next] == 0) {
      f = true;
      used[next] = 1;
      dfs(next, dep + 1, G, used);
      used[next] = 0;
    }
  }
  if (!f) {
    ans = max(ans, dep);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while (cin >> n, n) {
    ans = 0;
    vector<vector<ll>> G(n);
    for (int i = 0; i < n; i++) {
      ll x, y;
      cin >> x >> y;
      x--;
      y--;
      G[x].push_back(y);
      G[y].push_back(x);

      vector<int> used(n, 0);
      for (int i = 0; i < n; i++) {
        used[i] = 1;
        dfs(i, 1, G, used);
        used[i] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}