"Bzoj 4011" [HNOI2015] fall Memory Maple Sound

Source: Internet
Author: User
Tags in degrees

4011: [HNOI2015] Fall Memory Maple Sound

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

"Heng Yi, do you believe in the existence of souls?" 」

Guo Hengyi and Yiu Feng on the streets of the Maple Sound township. Looking at the sky Flying Red maple, Maple suddenly asked
Such a problem.
"Believe it." Or what are we, a regiment of meat? But for a soul ... And we can't see each other.
Come to your sister. 」
Heng Yat gave a slightly unreasonable answer. Maple smiled after listening.
"So have you looked closely at the Maple Leaf?" 」
Said, Feng Xi reached out and caught a falling maple leaf.
"In fact, every maple Leaf has a soul." Do you see that there are so many threads on the maple leaf? I heard that
There are some special places on the Maple leaf, just like people's acupoints. The veins are connected between these points.
Maple soul flows through each leaf root, along these veins, slowly diffuse into the acupoints, qinru the whole maple leaf.
Also for this reason, the context is one-way, the soul can not be backwards to sneak back. 」
Heng Yat indefinitely nodded at the location. Maple went on to say it.
"It is because of the soul, each maple leaf will be different." And it's because of the soul, each piece of maple
Leaves are also in the likeness of their source of the maple, even the context of the formation of a tree look. But if you look closely,
It is found that there are other very fine contexts outside the choroid tree. Although these veins are not on the tree,
But their direction is also in the direction of the soul flow, there will never be a return to the soul of the circuit. 」
Heng Yi seems to think of something suddenly.
"Wouldn't these contexts be able to replace existing contexts and appear on the choroid tree?" 」
Maple closed her eyes.
"Yes, that's it." The choroid tree is not unique. As long as there are slight deviations, the choroid tree
May be poor miles, even in this same piece of maple leaf. Like our story, the end is not the only one.
As long as you change a small option, the story flow may be completely reversed. 」
"It's so deep ..."
Heng Yi stared at the red maple and said thoughtfully. Maple continued.
"More than that," he said. All the veins do not exist forever, and will not disappear forever. Regardless of the context
The veins in the tree, or the small veins outside, are so. The veins of existence may break away, disappear
The context may also be connected again. All things are in eternal change, and the fetters between people are also. Maybe
One day, our fetters with you will be the same as the context, was ruthlessly cut off. Maybe we will be
As "the passer-by of Maple Sound Township". Perhaps all this will be inevitable, is determined by the soul of the maple tree ... "
Maple's corner of the eyes of a few drops of crystal clear tears. Heng Yi looked at such a maple, she carried her into her arms.
"Don't think so, maple. Even if the context is broken, there may also be a new choroid tree, also with maple
The roots of the tree are connected. In that case, our fetters are still there, just a little detour. No matter
How, I will not leave you. Because you are my life to seek, my true love Ah! 」
The two men's eyes were on. Maple smiled happily, burying her head in the arms of Heng Yi. From a distant mountain
Maple in the forest, came the sound of maple.
"Problem description"
Assume that there are n points on the Maple Leaf, the number of the acupoints is 1 ~ N. There are several connected contexts.
With these points. The acupoints and veins form a direction-free graph-called the choroid (example 1),
Bit of the number so that the point 1 is not connected to its context from other points, that is, the point 1 has connected to the vein;
As the above story shows, there is a tree-shaped graph of the direction-free graph, which is the inclusion of the Acupoint 1 root
All n points of a tree-called the choroid tree (example 2 and Figure 3 gives the tree is shown in Figure 1 given
Sub-graph of the context diagram); It is noteworthy that the choroid tree scheme in the context diagram may have many possibilities,
Fig. 2 and 3 are two choroid tree schemes for the context graph given in Figure 1.

The formalization of the choroid tree is defined as: the choroid tree with acupoints R as root is composed of all n acupoints on the Maple Leaf and n
-1 lines, there is no ring in the choroid tree, there is no link from a point to its own context, and for
Each point s on the Maple Leaf has a unique choroid path that is contained within the choroid tree, making it
R start along this path to the acupoints s.
Now add a different context to the context (Note: Connect 2 acupoints But the direction is not
The context of the same is different, for example, from the point 3 to 4 of the context and from 4 to 3 of the context is a different context,
Therefore, Figure 1 does not add a context from 3 to 4, but can add a context from 4 to 3, the new context
It can be connected from one point to the other (for example, in Figure 1 you can add a context from 4 to 4). Original Context Chart
The new context that is added to this new context may appear in the context of the loop.
Please find out the number of the choroid tree with Acupoint 1 as the root of the new context after adding this context.
Because there may be too many scenarios, output the results of the scenario number 1,000,000,007 modulo.
Input

The first line of the input file contains four integers n, m, x, and Y, which in turn represent the number of points on the Maple Leaf, the Pulse

The number of contacts, and the context to be added is from the point x to the acupoints y.
Next m line, two integers per line, separated by a space, representing a context. Two integers on line i
For the UI and VI, the context of the section I is connected from the point UI to the Acupoint VI.
Output

The output line, in order to add a vein from the point x connected to the acupoints y, the maple leaf on the root of the Acupoint 1

The result of the scheme number of the network tree to 1,000,000,007 modulo.
Sample Input

4 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.

Thinking problem +DP.

Since I learned the smallest tree diagram (see "POJ 3164"), it is easy to know that the graph required by the topic is equivalent to finding an edge for each point (except for 1), so the answer to the initial case is the product of each point (except 1) in degrees.

If you add an edge:
① if this side points to 1, then he is meaningless, and the answer is still the product of the penetration of points other than 1.

② If this side does not point to 1, continue to calculate the number of scenarios according to the calculation method will exist illegal (may exist ring), then we use p To calculate the number of scenarios in which the loop exists (note that direct DFS will time out!!) )
The ring must contain the newly added edge, and the number of schemes containing the ring is equal to Y To X Number of paths (no points on the path), except ? Y The graph of this edge is a topological order, and we follow the topological order to do the DP.

#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>Qstructedge{intY,ne;} e[m*2];intn,m,x,y,tot=0, Id[m],du[m],d[m],ts[m],h[m]; LL Ni[m],pre[m],f[m];voidReadint&AMP;TMP) {tmp=0;CharCh=getchar (); for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ()); for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*Ten+ch-' 0 ';}voidAddedge (intXintY) {e[++tot].y=y;    E[TOT].NE=H[X];    H[x]=tot; du[y]++;} ll Pow (ll X,intN) {LL ans=1, B=x; while(n) {if(n&1) Ans=ans*b%mod;        B=b*b%mod; n>>=1; }returnAns;}voidTopsort () { for(intI=1; i<=n;i++) {d[i]=du[i];if(!du[i]) Q.push (i); } d[y]--; while(!q.empty ()) {intX=q.front ();        Q.pop (); F[x]=f[x]*pow (du[x],mod-2)%mod; for(intI=h[x];i;i=e[i].ne) {intY=E[I].Y;            F[y]= (f[y]+f[x])%mod; d[y]--;if(!d[y]) Q.push (y); }    }}intMain () {read (n), read (m), read (X), read (Y); du[y]++; for(intI=1; i<=m;i++) {intx, y;        Read (x), read (y);    Addedge (x, y); } LL ans=1; for(intI=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;}

"Bzoj 4011" [HNOI2015] fall Memory Maple Sound

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.