牛客小白月赛21 D (拓扑图路径计数 +dp )

题目链接
牛客小白月赛21 D (拓扑图路径计数 +dp )_第1张图片
解题报告:
花里胡哨的描述,简单点就是说 1-> n 的路径数(注意题意的任意时间发送)。

直接上拓扑排序dp计数。
dp[i] : 表示到达 i 点的方案数

#define first f
#define second s
#define ll long long
#define pb push_back
#define pii pair
#define sl(p) strlen(p)
#define SZ(p) p.size()
#include 
#define lb lower_bound
#define ub upper_bound
#define mem(a,b) memset(a,b,sizeof(a))
#define fast_cin  ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
const int MOD=20010905;
const int maxn=1e6+5;
const int inf=INT_MAX;

struct TMD{
    int to,next;
}edge[maxn<<1];
int head[maxn],cnt,in[maxn];
void add(int from,int to)
{
    edge[++cnt]={to,head[from]};
    head[from]=cnt;
}
int n,m,dp[maxn];

int main()
{
    mem(head,-1);
    queue<int>q;
    int u,v,w;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&u,&v,&w);
        add(u,v);add(v,u);
        in[v]++;
    }
    q.push(1);
    dp[1]=1;
    while(!q.empty()){
        int pp=q.front();q.pop();
        for(int i=head[pp];~i;i=edge[i].next){
            int to=edge[i].to;
            in[to]--;
            if(!in[to]) q.push(to);
            dp[to]=(dp[to]+dp[pp])%MOD;
        }

    }
    printf("%d\n",dp[n]);
    return 0;
}

你可能感兴趣的:(拓扑)