Problem p00119 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

long long int memo[1 << 20];
long long int getans(bitset<20> &bs, vector<int> &nums,
                     vector<vector<int>> &edges) {
  if (memo[bs.to_ulong()] != -1)
    return memo[bs.to_ulong()];
  if (bs.count()) {
    long long int ans = 0;
    for (int i = 0; i < nums.size(); ++i) {
      if (!nums[i] && !bs[i]) {
        bs[i] = true;
        for (auto e : edges[i]) {
          nums[e]--;
        }
        ans += getans(bs, nums, edges);
        bs[i] = false;
        for (auto e : edges[i]) {
          nums[e]++;
        }
      }
    }
    return memo[bs.to_ulong()] = ans;
  } else {
    return memo[bs.to_ulong()] = 1;
  }
}
vector<int> topo_sort(const vector<vector<int>> &edges) {
  int size_ = edges.size();
  vector<int> nums(size_);
  for (const auto &es : edges) {
    for (auto e : es) {
      nums[e]++;
    }
  }
  queue<int> que;
  for (int i = 0; i < size_; ++i) {
    if (!nums[i]) {
      que.push(i);
    }
  }
  vector<int> topo;
  while (!que.empty()) {
    int ver(que.front());
    topo.push_back(ver);
    que.pop();
    for (auto e : edges[ver]) {
      nums[e]--;
      if (!nums[e])
        que.push(e);
    }
  }
  if (topo.size() == size_)
    return topo;
  else
    return vector<int>();
}

int main() {
  for (int i = 0; i < (1 << 20); ++i) {
    memo[i] = -1;
  }
  int M, N;
  cin >> M >> N;
  vector<vector<int>> edges(M);
  vector<int> nums(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    edges[x].push_back(y);
    nums[y]++;
  }
  bitset<20> bs((1 << N) - 1);
  long long int ans = getans(bs, nums, edges);
  auto v = topo_sort(edges);
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

long long int memo[1 << 20];
long long int getans(bitset<20> &bs, vector<int> &nums,
                     vector<vector<int>> &edges) {
  if (memo[bs.to_ulong()] != -1)
    return memo[bs.to_ulong()];
  if (bs.count()) {
    long long int ans = 0;
    for (int i = 0; i < nums.size(); ++i) {
      if (!nums[i] && !bs[i]) {
        bs[i] = true;
        for (auto e : edges[i]) {
          nums[e]--;
        }
        ans += getans(bs, nums, edges);
        bs[i] = false;
        for (auto e : edges[i]) {
          nums[e]++;
        }
      }
    }
    return memo[bs.to_ulong()] = ans;
  } else {
    return memo[bs.to_ulong()] = 1;
  }
}
vector<int> topo_sort(const vector<vector<int>> &edges) {
  int size_ = edges.size();
  vector<int> nums(size_);
  for (const auto &es : edges) {
    for (auto e : es) {
      nums[e]++;
    }
  }
  queue<int> que;
  for (int i = 0; i < size_; ++i) {
    if (!nums[i]) {
      que.push(i);
    }
  }
  vector<int> topo;
  while (!que.empty()) {
    int ver(que.front());
    topo.push_back(ver);
    que.pop();
    for (auto e : edges[ver]) {
      nums[e]--;
      if (!nums[e])
        que.push(e);
    }
  }
  if (topo.size() == size_)
    return topo;
  else
    return vector<int>();
}

int main() {
  for (int i = 0; i < (1 << 20); ++i) {
    memo[i] = -1;
  }
  int M, N;
  cin >> M >> N;
  vector<vector<int>> edges(M);
  vector<int> nums(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    edges[x].push_back(y);
    nums[y]++;
  }
  bitset<20> bs((1 << N) - 1);
  // long long int ans = getans(bs, nums, edges);
  auto v = topo_sort(edges);
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] + 1 << endl;
  }
  return 0;
}