P4017 最大食物链计数(记忆化搜索)

这篇博客主要涉及了一种基于深度优先搜索(DFS)的计数问题。程序代码中,作者定义了一个DFS函数来遍历一棵树,并计算每个没有被其他节点直接或间接食用的节点的数量。输入包括节点数n和边数m,然后是边的连接信息。通过递归地遍历所有未被食用的节点,累加计数,最后返回答案。这个算法在信息技术领域中属于图论和搜索算法的应用。
摘要由CSDN通过智能技术生成
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值