【BZOJ】4011: [HNOI2015]落忆枫音

时间:2022-01-01 20:08:45

题目链接:http://blog.csdn.net/popoqqq/article/details/45194103


写代码的时候也没有很清晰....具体看这里

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1001000
#define md 1000000007
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,s,y,f[maxn],du[maxn],indu[maxn],ans,t;
struct node
{
llg po;
bool operator<(const node&a) const{
return du[a.po]<du[po];
}
}; vector<llg>a[maxn]; priority_queue<llg>q; llg inv(llg x)
{
llg b=md-;
llg sum=;
while (b)
{
if (b&) sum*=x,sum%=md;
x*=x; x%=md;
b/=;
}
return sum;
} void init()
{
llg x,y;
cin>>n>>m>>s>>t;
du[t]++,indu[t]++;
for (llg i=;i<=m;i++)
{
scanf("%lld%lld",&x,&y);
a[x].push_back(y),du[y]++; indu[y]++;
}
ans=;
f[t]=;
for (llg i=;i<=n;i++) ans*=indu[i],ans%=md,f[t]*=indu[i],f[t]%=md;
} void DP()
{
llg x;
for (llg i=;i<=n;i++) if (!du[i]) q.push(i);
du[t]--;
while (!q.empty())
{
x=q.top(); q.pop();
f[x]*=inv(indu[x]); f[x]%=md;
llg w=a[x].size();
for (llg i=;i<w;i++)
{
llg y=a[x][i];
f[y]+=f[x],f[y]%=md;
du[y]--;
if (!du[y]) q.push(y);
}
}
} int main()
{
yyj("tree");
init();
if (t==) {cout<<ans; return ;}
DP();
ans-=f[s]; ans+=md; ans%=md;
cout<<ans;
return ;
}