#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
const int mod = 80112002;
int n, m;
ll ans;
vector<int> eat[5005];
int eater[5005];
int dp[5005];
int ate[5005];
int dfs(int x) {
int num = 0;
if (!ate[x]) {
return 1;
}
if (dp[x]) {
return dp[x];
}
for (auto e : eat[x]) {
(num += dfs(e)) %= mod;
}
return dp[x] = num;
}
int main() {
int t1, t2;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &t1, &t2);
eat[t1].push_back(t2);
eater[t2] = 1;
ate[t1] = 1;
}
for (int i = 1; i <= n; i++) {
if (!eater[i]) {
(ans += dfs(i)) %= mod;
}
}
printf("%lld", ans);
return 0;
}
P4017 最大食物链计数(记忆化搜索)
最新推荐文章于 2024-10-01 23:05:47 发布
这篇博客主要涉及了一种基于深度优先搜索(DFS)的计数问题。程序代码中,作者定义了一个DFS函数来遍历一棵树,并计算每个没有被其他节点直接或间接食用的节点的数量。输入包括节点数n和边数m,然后是边的连接信息。通过递归地遍历所有未被食用的节点,累加计数,最后返回答案。这个算法在信息技术领域中属于图论和搜索算法的应用。
摘要由CSDN通过智能技术生成