二十分,似乎少加了点什么
查看原帖
二十分,似乎少加了点什么
1070296
lieyang25楼主2024/4/9 21:44
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int p = 80112002, N = 5010;
vector<ll> g[N];
int n,m;
ll ans;
int ind[N];
int dp[N];
void topo()
{
    queue<int> q;
    for(int i=1;i<=n;i++)
        if(!ind[i])
            q.push(i),dp[i]=1;

    while(q.size())
    {
        ll x = q.front();q.pop();
        if(g[x].empty()) 
        {
          ans=(ans+dp[x])%p;
          continue;
        }
        for(const auto&y:g[x])
        {
            dp[y] += dp[x]; 
            ind[y]--;
            if(!ind[y])
                q.push(y);
        }
    }
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;  cin>>x>>y;
        ind[y]++;
        g[x].push_back(y);
    }
    topo();
    cout<<ans;
    return 0;
}
2024/4/9 21:44
加载中...