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

#include <bits/stdc++.h>
/*
 */

using namespace std;

#define mod 1000000007LL

template <typename T> class UF {
public:
  UF() { size = 0; }

  //???????????\\??????
  void push(T a) {
    M[a] = size;
    V.push_back(size);
    rank.push_back(0);
    C.push_back(1);
    size++;
  }

  //????????°??????????????????
  void unite(T a, T b) {
    long long int x = find(M[a]);
    long long int y = find(M[b]);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      V[x] = y;
      C[y] += C[x];
    } else {
      V[y] = x;
      C[x] += C[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  void merge(T a, T b) { unite(a, b); }

  //????????°??????????????????????????????????????????
  bool same(T a, T b) { return find(M[a]) == find(M[b]); }

  //????????°????????????????????°?????????
  long long int group() {
    set<long long int> S;
    for (long long int i = 0; i < V.size(); i++) {
      S.insert(find(V[i]));
    }
    return S.size();
  }

  //??°?????????????±???????????????°?????????
  long long int count(T a) { return C[find(M[a])]; }

  //????±???????????????°????????????ID?????????
  long long int groupID(T a) { return find(M[a]); }

  //????????°?????????????????????????????????
  vector<T> friendList(T a) {
    long long int inputID = find(M[a]);
    vector<T> ret;
    for (auto now : M) {
      if (find(now.second) == inputID) {
        ret.emplace_back(now.first);
      }
    }
    return ret;
  }

private:
  vector<long long int> C, V, rank;
  map<T, long long int> M;
  long long int size;

  //?\\?????????§?????????
  long long int find(long long int x) {
    if (V[x] == x)
      return x;
    else
      return V[x] = find(V[x]);
  }
};

int main() {
  long long int N, M;
  cin >> N >> M;
  UF<long long int> uf;
  for (size_t i = 0; i < N; i++) {
    uf.push(i);
  }
  vector<vector<long long int>> D(N);
  vector<vector<long long int>> F(N);
  for (size_t i = 0; i < M; i++) {
    long long int x, y;
    cin >> x >> y;
    x--;
    y--;
    D[x].push_back(y);
    F[y].push_back(x);
    uf.merge(x, y);
  }

  function<long long int(long long int)> f =
      [&](long long int idx) -> long long int {
    long long int ret = 1;
    for (auto x : F[idx]) {
      ret *= f(x);
      ret %= mod;
    }
    return ret + 1;
  };
  long long int ans = 1;
  set<long long int> checked;
  vector<bool> checkbool(N);
  for (size_t i = 0; i < N; i++) {
    if (!checked.count(uf.groupID(i))) {
      checked.insert(uf.groupID(i));
      auto group = uf.friendList(i);

      long long int now = i;
      while (!checkbool[now] && D[now].size()) {
        checkbool[now] = true;
        now = D[now][0];
      }

      if (D[now].size()) {
        auto now_ = D[now][0];
        set<long long int> SLL;
        SLL.insert(now);
        long long int prev;
        while (now_ != now) {
          SLL.insert(now_);
          prev = now_;
          for (auto x : F[now_]) {
            if (x != prev) {
              F[now].push_back(x);
            }
          }
          now_ = D[now_][0];
        }
        F[now].erase(remove_if(F[now].begin(), F[now].end(),
                               [&](auto i) { return SLL.count(i); }),
                     F[now].end());
        now_ = D[now][0];
      }
      ans *= f(now);
      ans %= mod;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
/*
 */

using namespace std;

#define mod 100000007LL

template <typename T> class UF {
public:
  UF() { size = 0; }

  //???????????\\??????
  void push(T a) {
    M[a] = size;
    V.push_back(size);
    rank.push_back(0);
    C.push_back(1);
    size++;
  }

  //????????°??????????????????
  void unite(T a, T b) {
    long long int x = find(M[a]);
    long long int y = find(M[b]);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      V[x] = y;
      C[y] += C[x];
    } else {
      V[y] = x;
      C[x] += C[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  void merge(T a, T b) { unite(a, b); }

  //????????°??????????????????????????????????????????
  bool same(T a, T b) { return find(M[a]) == find(M[b]); }

  //????????°????????????????????°?????????
  long long int group() {
    set<long long int> S;
    for (long long int i = 0; i < V.size(); i++) {
      S.insert(find(V[i]));
    }
    return S.size();
  }

  //??°?????????????±???????????????°?????????
  long long int count(T a) { return C[find(M[a])]; }

  //????±???????????????°????????????ID?????????
  long long int groupID(T a) { return find(M[a]); }

  //????????°?????????????????????????????????
  vector<T> friendList(T a) {
    long long int inputID = find(M[a]);
    vector<T> ret;
    for (auto now : M) {
      if (find(now.second) == inputID) {
        ret.emplace_back(now.first);
      }
    }
    return ret;
  }

private:
  vector<long long int> C, V, rank;
  map<T, long long int> M;
  long long int size;

  //?\\?????????§?????????
  long long int find(long long int x) {
    if (V[x] == x)
      return x;
    else
      return V[x] = find(V[x]);
  }
};

int main() {
  long long int N, M;
  cin >> N >> M;
  UF<long long int> uf;
  for (size_t i = 0; i < N; i++) {
    uf.push(i);
  }
  vector<vector<long long int>> D(N);
  vector<vector<long long int>> F(N);
  for (size_t i = 0; i < M; i++) {
    long long int x, y;
    cin >> x >> y;
    x--;
    y--;
    D[x].push_back(y);
    F[y].push_back(x);
    uf.merge(x, y);
  }

  function<long long int(long long int)> f =
      [&](long long int idx) -> long long int {
    // cout << idx << endl;
    long long int ret = 1;
    for (auto x : F[idx]) {
      ret *= f(x);
      ret %= mod;
    }
    return ret + 1;
  };
  long long int ans = 1;
  set<long long int> checked;
  vector<bool> checkbool(N);
  for (size_t i = 0; i < N; i++) {
    if (!checked.count(uf.groupID(i))) {
      checked.insert(uf.groupID(i));
      auto group = uf.friendList(i);

      long long int now = i;
      while (!checkbool[now] && D[now].size()) {
        now = D[now][0];
        checkbool[now] = true;
      }

      if (D[now].size()) {
        auto now_ = D[now][0];
        set<long long int> SLL;
        SLL.insert(now);
        long long int prev;
        while (now_ != now) {
          SLL.insert(now_);
          prev = now_;
          for (auto x : F[now_]) {
            if (x != prev) {
              F[now].push_back(x);
            }
          }
          now_ = D[now_][0];
        }
        F[now].erase(remove_if(F[now].begin(), F[now].end(),
                               [&](auto i) { return SLL.count(i); }),
                     F[now].end());
        now_ = D[now][0];
      }
      ans *= f(now);
      ans %= mod;
    }
  }
  cout << ans << endl;
}