L3-025 那就别担心了 (30 分)
作者:互联网
题目链接
dfs+记忆化搜索
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<int> a[510];
long long int w[510];
int End, Start, flag = 1;
long long int dfs(int u) {
if (w[u] != -1) return w[u];//如果这个点之前遍历过,直接返回路径条数
long long int sum = 0;
for (int i = 0; i < a[u].size(); i++) {
if (w[a[u][i]] == -1) dfs(a[u][i]);//如果这个点没遍历过就继续搜索
sum += w[a[u][i]];//统计路径条数
}
if (sum == 0) flag = 0;//如果一个点出度为0,且不为终点(终点出度也为0)的话就代表不是逻辑自洽的,flag标记为0
return w[u] = sum;
}
int main(int argc, char const *argv[]) {
int n, m, que[510], k = 0;
memset(w, -1, sizeof(w));//初始化全为-1,代表没遍历过
cin >> n >> m;
while (m--) {
int x, y;
cin >> x >> y;
a[x].push_back(y);
}
cin >> Start >> End;
w[End] = 1;//标记终点
printf("%lld ", dfs(Start));
if (flag)//逻辑是否自洽
printf("Yes\n");
else
printf("No\n");
return 0;
}
标签:dfs,int,sum,30,long,025,flag,L3,include 来源: https://blog.csdn.net/qq_34035720/article/details/115421034