DFS
#include <iostream>
#include <vector>
using namespace std;
const int MAX_V = 101;
vector<int> map[MAX_V];
int book[MAX_V];
int st, ed;
int ans = 0;
void dfs(int s) {
if (s == ed) {
ans++;
return;
}
for (auto i : map[s]) {
if (!book[i]) {
book[i] = 1;
dfs(i);
book[i] = 0;
}
}
}
int main() {
int V, m;
cin >> V >> m;
cin >> st >> ed;
while (m--) {
int a, b;
cin >> a >> b;
map[a].push_back(b);
}
dfs(st);
cout << ans;
}
拓扑排序+DP
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAX_V = 101;
vector<int> map[MAX_V], a;
int book[MAX_V], deg[MAX_V], dp[MAX_V][MAX_V], e[MAX_V][MAX_V];
int st, ed, V, m;
int ans = 0, flags, flage;
void topo() {
queue<int> q;
for (int i = 1; i <= V; i++) {
if (!deg[i]) {
q.push(i);
}
}
while (q.size()) {
int tmp = q.front();
q.pop();
a.push_back(tmp);
if (tmp == st)
flags = a.end() - a.begin() - 1;
if (tmp == ed)
flage = a.end() - a.begin() - 1;
for (auto i : map[tmp]) {
deg[i]--;
if (!deg[i])
q.push(i);
}
}
}
int main() {
cin >> V >> m;
cin >> st >> ed;
while (m--) {
int a, b;
cin >> a >> b;
e[a][b]++;
map[a].push_back(b);
deg[b]++;
}
topo();
for (int i = 1; i <= V; i++)
dp[i][i] = 1;
for (int i = flags + 1; i <= flage; i++) {
for (int j = flags; j < i; j++) {
if (e[a[j]][a[i]])
dp[st][a[i]] += dp[st][a[j]] * e[a[j]][a[i]];
}
}
cout << dp[st][ed];
}