Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class TVertex {
  int FIndex;
  int FCommuniyId;
  std::vector<TVertex *> FList;

public:
  TVertex(int Index) : FIndex(Index), FCommuniyId(0) {}
  void AddPath(TVertex *Vertex) { FList.push_back(Vertex); }
  void SetCommunity(int CommunityId) {
    if (FCommuniyId)
      return;
    FCommuniyId = CommunityId;
    for (std::vector<TVertex *>::iterator It = FList.begin(); It != FList.end();
         ++It) {
      TVertex *V = *It;
      V->SetCommunity(CommunityId);
    }
  }
  int CommunityId() const { return FCommuniyId; }
};
class TGraph {
  std::vector<TVertex *> FTable;
  void Init(int n) {
    assert(n > 0);
    FTable.resize(n);
    for (int i = 0; i < n; ++i) {
      FTable[i] = new TVertex(i);
    }
  }

public:
  TGraph(int n) { Init(n); }

  void SetupVertex(std::istream &in) {
    int Index;
    in >> Index;
    assert(Index >= 1);
    assert(Index <= FTable.size());
    int k;
    in >> k;
    for (int i = 0; i < k; ++i) {
      int v;
      in >> v;
      FTable[Index]->AddPath(FTable[v]);
    }
  }
  void MakeFriend(int s, int t) {
    assert(s < FTable.size());
    assert(t < FTable.size());
    FTable[s]->AddPath(FTable[t]);
    FTable[t]->AddPath(FTable[s]);
  }
  void SetCommunity() {
    for (std::size_t i = 0; i < FTable.size(); ++i) {
      FTable[i]->SetCommunity(i + 1);
    }
  }
  bool IsSameCommunity(int s, int t) const {
    assert(s < FTable.size());
    assert(t < FTable.size());
    return FTable[s]->CommunityId() == FTable[t]->CommunityId();
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  TGraph Graph(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    std::cin >> s >> t;
    Graph.MakeFriend(s, t);
  }
  int q;
  std::cin >> q;
  Graph.SetCommunity();
  for (int i = 0; i < q; ++i) {
    int s, t;
    std::cin >> s >> t;
    std::cout << (Graph.IsSameCommunity(s, t) ? "yes" : "no") << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef std::vector<bool> TCheckList;
class TVertex {
  int FIndex;
  std::vector<TVertex *> FList;

public:
  TVertex(int Index) : FIndex(Index) {}
  void AddPath(TVertex *Vertex) { FList.push_back(Vertex); }
  bool IsFriend(const TVertex *Target, TCheckList &CheckList) const {
    assert(FIndex < CheckList.size());
    if (CheckList[FIndex])
      return false;
    CheckList[FIndex] = true;
    for (std::vector<TVertex *>::const_iterator It = FList.begin();
         It != FList.end(); ++It) {
      const TVertex *V = *It;
      if (V == Target)
        return true;
      if (V->IsFriend(Target, CheckList)) {
        return true;
      }
    }
    return false;
  }
};
class TGraph {
  std::vector<TVertex *> FTable;
  void Init(int n) {
    assert(n > 0);
    FTable.resize(n);
    for (int i = 0; i < n; ++i) {
      FTable[i] = new TVertex(i);
    }
  }

public:
  TGraph(int n) { Init(n); }

  void SetupVertex(std::istream &in) {
    int Index;
    in >> Index;
    assert(Index >= 1);
    assert(Index <= FTable.size());
    int k;
    in >> k;
    for (int i = 0; i < k; ++i) {
      int v;
      in >> v;
      FTable[Index]->AddPath(FTable[v]);
    }
  }
  void MakeFriend(int s, int t) {
    assert(s < FTable.size());
    assert(t < FTable.size());
    FTable[s]->AddPath(FTable[t]);
    FTable[t]->AddPath(FTable[s]);
  }
  bool IsFriend(int s, int t) const {
    assert(s < FTable.size());
    assert(t < FTable.size());
    TCheckList CheckList(FTable.size(), false);
    return FTable[s]->IsFriend(FTable[t], CheckList);
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  TGraph Graph(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    std::cin >> s >> t;
    Graph.MakeFriend(s, t);
  }
  int q;
  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    std::cin >> s >> t;
    std::cout << (Graph.IsFriend(s, t) ? "yes" : "no") << std::endl;
  }
  return 0;
}