[BZOJ 4011] [HNOI2015] Fu Yi Feng Yin, bzojhnoi2015

Source: Internet
Author: User

[BZOJ 4011] [HNOI2015] Fu Yi Feng Yin, bzojhnoi2015
4011: [HNOI2015] luyi fengyin

Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 244 Solved: 137
[Submit] [Status] [Discuss]
Description

「 Hengyi, do you believe in the existence of the soul ?」

Guo hengyi and Yao fengqian walk along the streets of fengyin Township. Looking at the Red Maple, Feng Qian suddenly asked
This is a problem.
"Believe it. Otherwise, what are we, a ball of meat? If it weren't for a soul ...... We cannot see you again.
Come to your sister .」
Hengyi gave an answer with a slight margin. Feng Qian smiled after hearing it.
「 Have you observed the maple leaf carefully ?」
After all, Feng Qian reached out and caught a falling maple leaf.
"In fact, every piece of maple leaf has a soul. You see, isn't there so many things on the maple leaf? I heard that,
The maple leaf has some special locations, just like the human acupoints. The context is connected between these acupoints.
The soul of the maple tree flows through the roots of each piece of maple leaf. along these traces, the maple leaf is slowly flushed into the acupoint and refreshed into the whole piece.
For this reason, the context is only one-way, and the soul cannot slide back .」
Hengyi nodded his head. Feng Qian went on to talk about it.
「 It is because of the soul that every piece of maple leaf is different. Because of the soul
Ye is also like the original maple tree, and even the context forms a tree. However,
We will find that there are other very fine contexts in addition to the context tree. Although these contexts are not on the tree,
But their direction also follows the flow of the soul, and there will never be a loop that may bring the soul back .」
Heng Yi suddenly thought of something.
「 Can these contexts Replace the existing ones and appear on the context tree ?」
Feng Qian closed his eyes.
「 Yes, that's it. The context tree is not unique. As long as there are slight deviations, the context tree will
It may be a thousand miles away, even on the same maple leaf. Like our story, the ending is not the only one.
As long as a small option is changed, the story process may be completely reversed .」
「 It's really esoteric ......」
Heng Yi stared at this red maple, thoughtfully said. Feng Qian continued.
"More than that. All contexts will not exist forever or disappear forever. Regardless of Context
The context on the tree is also a small context outside of the tree. The existing context may be disconnected and disappear.
The context may be connected again. All things are in constant changes, and the bond between people is also. Maybe
One day, we will be cut off from each other in the same way as the context. Maybe we will eventually
It is "a passthrough of fengyin township ". Perhaps all these will be inevitable, determined by the soul of the maple tree ......」
Feng Qian's eyes contain a few drops of crystal clear tears. Heng Yi looked at such Feng Qian and held her in her arms.
"Don't think so, Feng Qian. Even if the context is disconnected, there may be a new context tree
The root of the tree is connected. In this case, our embarrassment still exists, but it just takes a long journey. Regardless
I will not leave you. Because you are what I have spent my whole life searching for, my real love !」
The two looked at each other. Feng Qian smiled happily and buried his head in the arms of hengyi. From a distant mountain
Fenglin, Feng's voice came.
[Problem description]
Assume that there are n Acupoints on the maple leaf, and The acupoint number is 1 ~ N. There are several directed contextual connections
These acupoints. Acupoint and vein form a directed acyclic graph-called a context map (Example 1 ).
The number of bits indicates that acupoint 1 is not connected from other acupuncture points, that is, acupoint 1 is only connected;
From the story above, we can see that this directed acyclic graph has a tree subgraph, which contains the root of acupoint 1.
A tree of all n acupoints is called a context tree (for example, 2 and 3 are given in Fig 1 ).
Subgraphs of the context map). It is worth noting that the context tree scheme in the context map may have multiple possibilities. For example:
2 and 3 are the two context tree schemes of the context map given in Figure 1.

The context tree is formally defined as: the context tree based on Acupoint r is composed of all n acupoints and n
-There is no ring in the context tree and there is no context connecting from one acupoint to itself.
Each acupoint s on the maple leaf has a unique vein path contained in the context tree, allowing
R. This path can be followed to point s.
Now add a different context to the context map (Note: connect two acupoints but not in the direction)
The same context is different. For example, the context from Acupoint 3 to acupoint 4 is different from that from Acupoint 4 to acupoint 3,
Therefore, the context from 3 to 4 cannot be added in Figure 1, but from 4 to 3 can be added .)
It can be connected to itself from an acupuncture point (for example, in Figure 1, you can add a context from 4 to 4 ). Original context Map
The new context map after this new context is added may contain a context.
Please find the number of Route tree schemes with acupoint 1 as the root of the new context map after this context is added.
Because there may be too many solutions, please output the results of Modulo-zing the number of solutions to 1,000,000,007.
Input

The first line of the input file contains four integers n, m, x, and y, which represent the number of acupoints and pulse on the maple leaf in sequence.

The number of links and the context to be added are connected from x to y.
In the next m row, each line has two integers separated by spaces, representing a context. Two integers in row I
For ui and vi, indicates that the I-th vein is connected from The acupoint ui to The acupoint vi.
Output

Output a line, which is the vein that is added from point x to point y.

The result of the modulo operation on the number of nodes in the tree is 1,000,000,007.
Sample Input

4 4 3

1 2

1 3

2 4

3 2
Sample Output

3
HINT

For all test data, 1 <=n <= 100000, n-1 <= m <= min (200000, n (n-1)/2 ),

1 <= x, y, ui, vi <= n.

Train of Thought + dp.

Since I have learned the least tree structure (see [POJ 3164]), it is easy to know that the graph required by the question is equivalent to every vertex (except 1) find an inner edge (because there is no ring in the question). Therefore, the initial answer is the product of each vertex (except 1.

If an edge is added:
① If this edge points to 1, it makes no sense. The answer is still the product of the incoming degrees of points except 1.

② If this edge does not point to 1, then there will be an invalid number of solutions calculated based on the previous calculation method (the ring may exist), then we use Dp To calculate the number of solutions with loops (note that direct dfs will time out !!)
The ring must contain the newly added edge, and the number of solutions containing the ring is equal Y To X The number of entries in the path (any point on the non-path ). X−y The graph of this edge has a topological order. We can perform dp in the topological order.

#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#define mod 1000000007#define M 100005#define LL long long#include <queue>using namespace std;queue<int> q;struct edge{    int y,ne;}e[M*2];int n,m,X,Y,tot=0,id[M],du[M],d[M],ts[M],h[M];LL ni[M],pre[M],f[M];void read(int &tmp){    tmp=0;    char ch=getchar();    for (;ch<'0'||ch>'9';ch=getchar());    for (;ch>='0'&&ch<='9';ch=getchar())        tmp=tmp*10+ch-'0';}void Addedge(int x,int y){    e[++tot].y=y;    e[tot].ne=h[x];    h[x]=tot;    du[y]++;}LL Pow(LL x,int n){    LL ans=1,b=x;    while (n)    {        if (n&1) ans=ans*b%mod;        b=b*b%mod;        n>>=1;    }    return ans;}void Topsort(){    for (int i=1;i<=n;i++)    {        d[i]=du[i];        if (!du[i]) q.push(i);    }    d[Y]--;    while (!q.empty())    {        int x=q.front();        q.pop();        f[x]=f[x]*Pow(du[x],mod-2)%mod;        for (int i=h[x];i;i=e[i].ne)        {            int y=e[i].y;            f[y]=(f[y]+f[x])%mod;            d[y]--;            if (!d[y]) q.push(y);        }    }}int main(){    read(n),read(m),read(X),read(Y);    du[Y]++;    for (int i=1;i<=m;i++)    {        int x,y;        read(x),read(y);        Addedge(x,y);    }    LL ans=1;    for (int i=2;i<=n;i++)        ans=ans*du[i]%mod;    if (Y==1)    {        cout<<ans<<endl;        return 0;    }    f[Y]=ans;    Topsort();    ans=(ans-f[X])%mod+mod;    cout<<ans%mod<<endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.