-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path071-03.cpp
More file actions
77 lines (76 loc) · 1.44 KB
/
071-03.cpp
File metadata and controls
77 lines (76 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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;
}