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

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


 

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<cstring>
 8 #include<queue>
 9 using namespace std;
10 #define maxn 1001000
11 #define md 1000000007
12 #define llg long long 
13 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
14 llg n,m,s,y,f[maxn],du[maxn],indu[maxn],ans,t;
15 struct node
16 {
17     llg po;
18     bool operator<(const node&a) const{
19         return du[a.po]<du[po];
20     }
21 };
22 
23 vector<llg>a[maxn];
24 
25 priority_queue<llg>q;
26 
27 llg inv(llg x)
28 {
29     llg b=md-2;
30     llg sum=1;
31     while (b)
32     {
33         if (b&1) sum*=x,sum%=md;
34         x*=x; x%=md;
35         b/=2;
36     }
37     return sum;
38 }
39 
40 void init()
41 {
42     llg x,y;
43     cin>>n>>m>>s>>t;
44     du[t]++,indu[t]++;
45     for (llg i=1;i<=m;i++)
46     {
47         scanf("%lld%lld",&x,&y);
48         a[x].push_back(y),du[y]++; indu[y]++;
49     }
50     ans=1;
51     f[t]=1;
52     for (llg i=2;i<=n;i++) ans*=indu[i],ans%=md,f[t]*=indu[i],f[t]%=md;
53 }
54 
55 void DP()
56 {
57     llg x;
58     for (llg i=1;i<=n;i++) if (!du[i]) q.push(i);
59     du[t]--; 
60     while (!q.empty())
61     {
62         x=q.top(); q.pop();
63         f[x]*=inv(indu[x]); f[x]%=md;
64         llg w=a[x].size();
65         for (llg i=0;i<w;i++)
66         {
67             llg y=a[x][i];
68             f[y]+=f[x],f[y]%=md;
69             du[y]--;
70             if (!du[y]) q.push(y);
71         }
72     }
73 }
74 
75 int main()
76 {
77     yyj("tree");
78     init();
79     if (t==1) {cout<<ans; return 0;}
80     DP();
81     ans-=f[s]; ans+=md; ans%=md;
82     cout<<ans;
83     return 0;
84 }

 

posted @ 2017-02-22 09:10  №〓→龙光←  阅读(136)  评论(0编辑  收藏  举报