# bzoj4011[HNOI2015]落忆枫音

http://www.lydsy.com/JudgeOnline/problem.php?id=4011

$\sum\limits_{S是y到x的任意一条路径}\sum\limits_{j\notin S}indegree[j]$

$=\sum\limits_{S是y到x的任意一条路径}\frac{\sum\limits_{j=1}^{n}indegree[j]}{\sum\limits_{j\in S}indegree[j]}$

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ，但不适用于poj

using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
{
int res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}
LL gll()
{
LL res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}

const int maxn=100000;
const int maxm=200000;
const LL Mod=1000000007;

int n,m,x,y;
int now,first[maxn+100];
struct Tedge{int v,next;}edge[maxm+100];
LL degree[maxn+100],_degree[maxn+100];
LL f[maxn+100],ans;

LL power(LL a,LL k){LL x=1;while(k){if(k&1)x=x*a%Mod;k>>=1;a=a*a%Mod;}return x;}

queue<int>Q;

int main()
{
freopen("bzoj4011.in","r",stdin);
freopen("bzoj4011.out","w",stdout);
int i;
n=gint();m=gint();x=gint();y=gint();
now=-1;mmst(first,-1);
ans=1;
re(i,1,n)_degree[i]=degree[i];
degree[y]++;
re(i,2,n)ans=ans*degree[i]%Mod;
if(y==1){cout<<ans<<endl;return 0;}
f[y]=ans;
re(i,1,n)if(_degree[i]==0)Q.push(i);
while(!Q.empty())
{
int u=Q.front(),v;Q.pop();
for(i=first[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v)
{
(f[v]+=f[u])%=Mod;
_degree[v]--;
if(_degree[v]==0)
{
(f[v]*=power(degree[v],Mod-2))%Mod;
Q.push(v);
}
}
}
ans-=f[x];
ans=(ans%Mod+Mod)%Mod;
cout<<ans<<endl;
return 0;
}
View Code

posted @ 2015-11-11 10:07  maijing  阅读(236)  评论(0编辑  收藏  举报