求助
查看原帖
求助
1163457
UncleEric楼主2024/3/19 20:30

luogu 在线IDE #1数据 23ms 正确


提交结果:TLE*10

附代码:

#include<bits/stdc++.h>
using namespace std;

int mod=80112002,n,m,a,b,ans;
int root[5005];
vector<int> edge[5005];
long long dp[5005];

int dfs(int x){
	if(dp[x]) return dp[x];
	if(!edge[x].size()) return 1;
	for(int i=0;i<edge[x].size();i++){
		dp[x]+=dfs(edge[x][i]);
		dp[x]%=mod;
	}
	return dp[x];
}

inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)) if(ch=='-'){f=-1; ch=getchar();}
	while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48); ch=getchar();}
	return f*x;
}

int main(){
	n=read();m=read();
	while(m--){
		a=read();b=read();
		edge[a].push_back(b);
		root[b]=1;
	}
	for(int i=1;i<=n;i++){
		if(root[i]) continue;
		ans+=dfs(i);
		ans%=mod;
	}
	cout<<ans;
	return 0;
}
2024/3/19 20:30
加载中...