• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
magicat
博客园    首页    新随笔    联系   管理    订阅  订阅
P4017 最大食物链计数

P4017 最大食物链计数

建图,食物链从  入度为0的点  到  出度为0的点  为一条完整的食物链

在DAG上拓扑排序dp,每个结点 有多少种方式 从入度为0的点到达表示为  f[v]+=f[u]  u->v

当遍历到的点出度为0就将 f[v] 的值加入答案

#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;


int n,m;
const int mod=80112002;
vector<int> son[100010];
int d[100010];
int out[100010];
long long f[100010];
long long ans=0;
void bfs()
{ 
    queue<int> q;
    for(int i=1;i<=n;i++)   
        if(d[i]==0)
        {
            q.push(i);
            f[i]=1;
        }
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(auto v:son[u])
        {
            f[v]=(f[v]+f[u])%mod;
            if(--d[v]==0)
            {
                q.push(v);
                if(out[v]==0)
                    ans=(ans+f[v])%mod;
            }
        }
    }

    cout<<(ans%mod)<<endl;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;    cin>>x>>y;
        son[x].push_back(y);
        d[y]++,out[x]++;
    }
    bfs();
    return 0;
}

 

本文来自博客园,作者:magicat,转载请注明原文链接:https://www.cnblogs.com/magicat/p/16535206.html

posted on 2022-07-30 16:10  magicat  阅读(159)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3