0分求调
查看原帖
0分求调
1086963
aqua__楼主2024/4/6 02:38
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int N = 5010, M = 500010, mod = 80112002;

int n, m;
int h[N],e[M],ne[M],idx;
int cnt[N];
int ans;
int in[N]; //入度 
int out[N]; //出度 

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void toposort()
{
	queue<int> q;
	for(int i = 1; i <= n; i ++ )
	{
		if(in[i] == 0) q.push(i);
		cnt[i] = 1;
	}
	while(q.size())
	{
		int t = q.front();
		q.pop();
		for(int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			cnt[j] = (cnt[j] + cnt[t]) % mod;
			-- in[j] ;
			if(in[j] == 0)
			{
				q.push(j);
			}
		}
	}
}

int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof h);
	while(m -- )
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
		in[b] ++ ;
		out[a] ++ ;
	}
	toposort();   
	for(int i = 1; i <= n; i ++ )
	{
		if(!out[i]) ans = (ans + cnt[i]) % mod; 
	}
	cout << ans << endl;
	return 0;
}
2024/4/6 02:38
加载中...