PTA 那就别担心了

(DFS)记忆化搜索

1. 对于第一个问题我们只要在(dfs)时判断一个点如果出度为(0)，并且该点不是终点，那么说明从起点出发的该条路径无法到达终点

2. 对于第二个问题，我们考虑记忆化搜索(dp)解决

``````#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << "=" << x << endl
#define all(x) (x).begin(), (x).end()
#define int long long
#define mpk make_pair
#define endl "
"
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 5e2 + 10, M = 4e5 + 10;

int n, m;
vector<int> g[N];
int f[N]; // f[i]:代表从i出发到终点的路径数
int st, ed;
int du[N]; // 出度
bool flag;

int dfs(int u)
{
if (du[u] == 0 && u != ed)
flag = false;
if (f[u])
return f[u];
int t = 0;
for (auto v : g[u])
t += dfs(v);
return f[u] = t;
}

void solve()
{
cin >> n >> m;
for (int i = 1, u, v; i <= m; ++i)
{
cin >> u >> v;
g[u].push_back(v);
du[u]++;
}
cin >> st >> ed;
flag = true;
f[ed] = 1;
dfs(st);
cout << f[st] << " ";
if (flag == true)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
``````