-
Notifications
You must be signed in to change notification settings - Fork 0
/
a64_q1_permu_before.cpp
54 lines (43 loc) · 996 Bytes
/
a64_q1_permu_before.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 25;
int n;
vector <int> g[MAX_N];
bool visited[MAX_N];
int vec[MAX_N];
void solve(int i) {
if (i == n) {
for (int j = 0; j < n; j++) cout << vec[j] << ' ';
cout << '\n';
return;
}
for (int j = 0; j < n; j++) {
if (visited[j] == false) {
bool ok = true;
for (auto v : g[j]) ok &= visited[v];
visited[j] = true;
vec[i] = j;
if (ok == true) solve(i + 1);
visited[j] = false;
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
g[b].push_back(a);
}
for (int i = 0; i < n; i++) {
bool ok = true;
for (auto v : g[i]) ok &= visited[v];
visited[i] = true;
vec[0] = i;
if (ok == true) solve(1);
visited[i] = false;
}
return 0;
}