AtCoder problem 071

C++ solution:
// Time Complexity: O((N + M + K) * K)



#include <vector>

#include <iostream>

#include <functional>

using namespace std;

int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);

	int N, M, K;

	cin >> N >> M >> K;

	vector<vector<int> > G(N);

	vector<int> deg(N);

	for (int i = 0; i < M; ++i) {

		int a, b;

		cin >> a >> b; --a, --b;

		G[a].push_back(b);

		++deg[b];

	}

	vector<int> st;

	vector<int> perm(N, -1);

	vector<vector<int> > answer_list;

	function<bool(int)> dfs = [&](int depth) {

		if (depth == N) {

			answer_list.push_back(perm);

			return true;

		}

		if (st.empty()) {

			return false;

		}

		for (int i = int(st.size()) - 1; i >= 0; --i) {

			if (answer_list.size() == K) {

				break;

			}

			int x = st[i];

			st.erase(st.begin() + i);

			for (int j : G[x]) {

				--deg[j];

				if (deg[j] == 0) {

					st.push_back(j);

				}

			}

			perm[depth] = x;

			bool sign = dfs(depth + 1);

			if (!sign) {

				return false;

			}

			for (int j : G[x]) {

				if (deg[j] == 0) {

					st.pop_back();

				}

				++deg[j];

			}

			st.insert(st.begin() + i, x);

		}

		return true;

	};

	for (int i = 0; i < N; ++i) {

		if (deg[i] == 0) {

			st.push_back(i);

		}

	}

	dfs(0);

	if (answer_list.size() != K) {

		cout << -1 << endl;

	}

	else {

		for (vector<int> v : answer_list) {

			for (int i = 0; i < N; ++i) {

				if (i != 0) cout << ' ';

				cout << v[i] + 1;

			}

			cout << endl;

		}

	}

	return 0;

}


Python solution: N,M,K = map(int,input().split())
E = [[] for i in range(N)]
inIdx = [0] * N
for i in range(M):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  E[a].append(b)
  inIdx[b] += 1

topological_que = []
recursive_stack = []
for i in range(N):
  if inIdx[i] == 0:
    topological_que += [i]
recursive_stack.append([0,len(topological_que)])
topological_que += [0] * (N - len(topological_que))

ans = []
while len(recursive_stack) != 0:
  i = len(recursive_stack) - 1
  st = recursive_stack[-1]
  if i == N:
    ans.append([x for x in topological_que])
    recursive_stack.pop()
    if len(recursive_stack) != 0:
      prei = i-1
      pres = recursive_stack[-1]
      for e in E[topological_que[prei]]: inIdx[e] += 1
      topological_que[prei],topological_que[pres[0]] = topological_que[pres[0]],topological_que[prei]
      recursive_stack[-1][0] += 1
    continue
  if st[1] == i:
    # no valid answer
    break
  if st[0] >= st[1]:
    recursive_stack.pop()
    if len(recursive_stack) != 0:
      prei = i-1
      pres = recursive_stack[-1]
      for e in E[topological_que[prei]]: inIdx[e] += 1
      topological_que[prei],topological_que[pres[0]] = topological_que[pres[0]],topological_que[prei]
      pres[0] += 1
    continue
  if len(ans) == K:
    break

  pres = recursive_stack[-1]
  que_back = pres[1]
  topological_que[i] , topological_que[pres[0]] = topological_que[pres[0]] , topological_que[i]
  p = topological_que[i]
  for e in E[p]:
    inIdx[e] -= 1
    if inIdx[e] == 0:
      topological_que[que_back] = e
      que_back += 1
  recursive_stack.append([ i+1,que_back ])

if len(ans) != K:
  print(-1)
else:
  print("\n".join([" ".join([str(x+1) for x in a]) for a in ans]))