20分求调!!2AC 8MLE
查看原帖
20分求调!!2AC 8MLE
1028281
czxczx2010楼主2024/8/14 11:42
#include<bits/stdc++.h>
using namespace std;
#define mod 80112002
int head[5005],ver[500005],next1[500005];
int n,m,tot=0,dp[5005];
bool t1[5005],t2[5005];
long long ans;
queue<int>q;
void add(int a,int b){
	ver[++tot]=b;
	next1[tot]=head[a];
	head[a]=tot;
}
void bfs(int now){
	while(!q.empty())q.pop();
	q.push(now);
	dp[now]=(dp[now]+1)%mod;
	while(!q.empty()){
		int t=q.front();
		for(int i=head[t];i;i=next1[i]){
			int p=ver[i];
			dp[p]=(dp[p]+1)%mod;
			q.push(p);
		}
		q.pop();
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		add(x,y);
		t1[y]=1;
		t2[x]=1;
	}
	for(int i=1;i<=n;i++){
		if(t1[i]==0)bfs(i);
    }
    for(int i=1;i<=n;i++){
    	if(t2[i]==0)ans=(ans+dp[i])%mod;
	}
	cout<<ans;
}
2024/8/14 11:42
加载中...