bzoj 4011 分类: bzoj 2015-05-28 14:11 33人阅读 评论(0) 收藏


Dp。

参见大神的题解


#include<queue>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>

const long long Mod = 1e9+7;
const int MAXN = 1e5+5, MAXM = 2e5 + 5;
struct Edge{int v,next; Edge(int v = 0,int next = 0):v(v),next(next){}};

int n, m, s, t;
long long ans = 1, f[MAXN];
int rev[MAXM];
int rdu[MAXN], nrd[MAXN];
int head[MAXN];
Edge edge[MAXM];int el;

void NewEdge(int u,int v)
{
    edge[++el] = Edge(v,head[u]), head[u] = el;
}
void GetRev()
{
    rev[1] = 1;
    for(int i = 2; i <= m + 1; i++)
        rev[i]=(Mod - Mod/i)*rev[Mod%i]%Mod;
}
void Daopi()
{
    std::queue<int> line;

    f[t] = ans;
    for(int i = 1; i <= n; i++)
        if(!rdu[i]) line.push(i);

    while(!line.empty())
    {
        int t = line.front(); line.pop();
        f[t] = f[t] * rev[nrd[t]] % Mod;

        for(int i = head[t]; i ; i = edge[i].next)
        {
            int p = edge[i].v;
            f[p] = (f[p] + f[t]) % Mod;

            if(!--rdu[p]) line.push(p);
        }
    }

}
int main()
{
#ifndef ONLINE_JUDGE    
    freopen("bzoj4011.in","r",stdin);
    freopen("bzoj4011.out","w",stdout);
#endif

    std::cin >> n >> m >> s >> t;

    for(int i = 1, ui, vi; i <= m; i++)
    {
        scanf("%d%d",&ui,&vi), NewEdge(ui,vi);
        rdu[vi] ++, nrd[vi] ++;
    }
    nrd[t] ++;

    GetRev();

    for(int i = 2; i <= n;i ++)
        ans *= nrd[i], ans %= Mod;

    if(t != 1) Daopi(), ans = (ans - f[s] + Mod) % Mod;

    std::cout << ans << std::endl;


#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-05-28 14:11    阅读(119)  评论(0编辑  收藏  举报