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

#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

template <class T> T at(vector<T> v, int i) {
  return v[(i % (int)v.size() + v.size()) % v.size()];
}

typedef int Weight;
struct Edge {
  int from, to;
  Weight weight;
  Edge(int from, int to, Weight weight) : from(from), to(to), weight(weight) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Matrix adjacency(const Graph &graph) {
  int n = graph.size();
  Matrix distance(n, Array(n, 1e9));
  for (const Edges &edges : graph)
    for (const Edge &edge : edges) {
      distance[edge.from][edge.to] = edge.weight;
    }
  return distance;
}

void debug(const Graph &g) {
  for (const Edges &es : g)
    for (const Edge &e : es) {
      cerr << e.from << " " << e.to << " " << e.weight << endl;
    }
}

int solve(const Graph &graph, int v, bitset<100> bit) {
  int res = 1;
  for (const auto &edge : graph[v])
    if (!bit[edge.to]) {
      bit[edge.to] = true;
      res = max(res, solve(graph, edge.to, bit) + 1);
      bit[edge.to] = false;
    }
  return res;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    Graph graph(100);
    for (int i : range(n)) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      graph[a].emplace_back(a, b, 1);
      graph[b].emplace_back(b, a, 1);
    }
    int res = 1;
    for (int i : range(100)) {
      bitset<100> bit;
      bit[i] = true;
      res = max(res, solve(graph, i, bit));
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

template <class T> T at(vector<T> v, int i) {
  return v[(i % (int)v.size() + v.size()) % v.size()];
}

typedef int Weight;
struct Edge {
  int from, to;
  Weight weight;
  Edge(int from, int to, Weight weight) : from(from), to(to), weight(weight) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Matrix adjacency(const Graph &graph) {
  int n = graph.size();
  Matrix distance(n, Array(n, 1e9));
  for (const Edges &edges : graph)
    for (const Edge &edge : edges) {
      distance[edge.from][edge.to] = edge.weight;
    }
  return distance;
}

void debug(const Graph &g) {
  for (const Edges &es : g)
    for (const Edge &e : es) {
      cerr << e.from << " " << e.to << " " << e.weight << endl;
    }
}

int solve(const Graph &graph, int v, bitset<100> bit) {
  int res = 1;
  for (const auto &edge : graph[v])
    if (!bit[edge.to]) {
      bit[edge.to] = true;
      res = max(res, solve(graph, edge.to, bit) + 1);
      bit[edge.to] = false;
    }
  return res;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    Graph graph(n);
    for (int i : range(n)) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      graph[a].emplace_back(a, b, 1);
      graph[b].emplace_back(b, a, 1);
    }
    int res = 0;
    for (int i : range(n)) {
      bitset<100> bit;
      bit[i] = true;
      res = max(res, solve(graph, i, bit));
    }
    cout << res << endl;
  }
}