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

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode {
  std::list<TNode *> FNexts;
  int FDistance;

public:
  TNode() : FDistance(-1) {}

  int GetDistance() const { return FDistance; }

  void Push(TNode *New) { FNexts.push_back(New); }

  void Visit(int Distance) {
    if (FDistance >= 0 && Distance > FDistance)
      return;
    FDistance = Distance;
    for (std::list<TNode *>::iterator It = FNexts.begin(); It != FNexts.end();
         ++It) {
      (*It)->Visit(Distance + 1);
    }
  }
};

class TGraph {
  std::vector<TNode *> FNodes;

public:
  TGraph(int Count) : FNodes(Count + 1) {
    for (int i = 1; i < Count + 1; ++i) {
      FNodes[i] = new TNode();
    }
  }

  ~TGraph() {
    for (unsigned int i = 1; i < FNodes.size(); ++i) {
      delete FNodes[i];
    }
  }

  void InputNode() {
    int Index;
    std::cin >> Index;
    int BranchCount;
    std::cin >> BranchCount;
    for (int i = 0; i < BranchCount; ++i) {
      int Target;
      std::cin >> Target;
      FNodes[Index]->Push(FNodes[Target]);
    }
  }

  void BreathFirstSearch() { FNodes[1]->Visit(0); }

  int Distance(int Target) const { return FNodes[Target]->GetDistance(); }
};

int main() {
  int Count;
  std::cin >> Count;
  TGraph Graph(Count);
  for (int i = 0; i < Count; ++i) {
    Graph.InputNode();
  }
  Graph.BreathFirstSearch();

  for (int i = 0; i < Count; ++i) {
    std::cout << i + 1 << " " << Graph.Distance(i + 1) << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode {
  int FIndex;
  std::list<TNode *> FTarget;

public:
  TNode(int Index) : FIndex(Index) {}

  void Push(TNode *New) { FTarget.push_back(New); }

  int Distance(const TNode *Target) const {
    if (Target == this)
      return 0;
    if (std::find(FTarget.begin(), FTarget.end(), Target) != FTarget.end())
      return 1;
    int Minimum = -2;
    for (std::list<TNode *>::const_iterator It = FTarget.begin();
         It != FTarget.end(); ++It) {
      int Distance = (*It)->Distance(Target);
      if (Distance >= 0 && (Minimum > Distance || Minimum < 0)) {
        Minimum = Distance;
      }
    }
    return 1 + Minimum;
  }
};

class TGraph {
  std::vector<TNode *> FNodes;

public:
  TGraph(int Count) : FNodes(Count) {
    for (int i = 0; i < Count; ++i) {
      FNodes[i] = new TNode(i + 1);
    }
  }

  ~TGraph() {
    for (unsigned int i = 0; i < FNodes.size(); ++i) {
      delete FNodes[i];
    }
  }

  void InputNode() {
    int Index;
    std::cin >> Index;
    int BranchCount;
    std::cin >> BranchCount;
    for (int i = 0; i < BranchCount; ++i) {
      int Target;
      std::cin >> Target;
      FNodes[Index - 1]->Push(FNodes[Target - 1]);
    }
  }

  int MeasureDistance(int Start, int End) const {
    return FNodes[Start - 1]->Distance(FNodes[End - 1]);
  }
};

int main() {
  int Count;
  std::cin >> Count;
  TGraph Graph(Count);
  for (int i = 0; i < Count; ++i) {
    Graph.InputNode();
  }

  for (int i = 0; i < Count; ++i) {
    std::cout << i + 1 << " " << Graph.MeasureDistance(1, i + 1) << std::endl;
  }
  return 0;
}