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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#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()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

bool visited[100];
int g[100][100];
int sz[100];
int n;
bool flag[100];

int dfs(int now) {
  visited[now] = 1;
  flag[now] = 1;
  int res = 0;
  REP(i, sz[now]) {
    if (visited[g[now][i]])
      continue;
    res = max(res, dfs(g[now][i]));
  }
  visited[now] = 0;
  return res + 1;
}

int main() {
  while (cin >> n, n) {
    memset(g, 0, sizeof(g));
    int num[100];
    memset(num, 0, sizeof(num));
    memset(sz, 0, sizeof(sz));
    memset(flag, 0, sizeof(flag));
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      g[a][sz[a]++] = b;
      g[b][sz[b]++] = a;
      num[a]++;
      num[b]++;
    }
    int ans = 0;
    for (int i = 1; i < 100; ++i) {
      bool tmp[100];
      memcpy(tmp, flag, sizeof(flag));
      REP(j, 100) {
        if (num[j] == i && tmp[j] == 0) {
          ans = max(ans, dfs(j));
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#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()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

bool visited[100];
vector<vector<int>> g;
int n;
int dfs(int now) {
  visited[now] = 1;
  int res = 0;
  FOR(it, g[now]) {
    if (visited[*it])
      continue;
    res = max(res, dfs(*it));
  }
  visited[now] = 0;
  return res + 1;
}

int main() {
  while (cin >> n, n) {
    g = vector<vector<int>>(100);
    vector<int> num(100);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      g[a].push_back(b);
      g[b].push_back(a);
      num[a]++;
      num[b]++;
    }
    vector<bool> memo(n);
    vector<vector<int>> connected;
    REP(i, 100) {
      if (memo[i])
        continue;
      queue<int> Q;
      Q.push(i);
      vector<int> tmpv;
      while (!Q.empty()) {
        int now = Q.front();
        Q.pop();
        if (memo[now])
          continue;
        memo[now] = 1;
        tmpv.push_back(now);
        FOR(it, g[now]) {
          if (memo[*it])
            continue;
          Q.push(*it);
        }
      }
      if (tmpv.size() > 1)
        connected.push_back(tmpv);
    }
    int ans = 0;
    REP(i, connected.size()) {
      if (ans > connected[i].size())
        continue;
      int mi = INF;
      FOR(it, connected[i]) {
        // cout << *it << " ";
        mi = min(mi, num[*it]);
      }
      // cout << endl;
      REP(j, connected[i].size()) {
        if (num[connected[i][j]] == mi) {
          memset(visited, 0, sizeof(visited));
          // cout << "j " << connected[i][j] << endl;
          ans = max(ans, dfs(connected[i][j]));
        }
      }
    }
    cout << ans << endl;
  }
}