Hihocoder #1175: Sort by topology • Two

Source: Internet
Author: User

"Topic link": Click here~~

Time limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Little Hi and Little Ho's School campus network was hacked and put on the virus. This matter in the school BBS immediately aroused everyone's discussion, of course, small hi and small ho also involved in it. From what everyone knows, little Hi and Little ho finishing get the following information:

    • The backbone of the campus network is made up of n nodes (number 1). N) consisting of some one-way network connections between these nodes. If there is a network connection (U,V) linked to node U and node V, then node u can send information to node V, but node v cannot send information to node u through the link.
    • In the newly infected with the virus, the campus network immediately cut off some network links, just so that the remaining network connection does not exist ring, to avoid the node is repeatedly infected. In other words, the virus that spreads from node I will not go back to Node I.
    • When 1 viruses infect a node, it does not check that the node is infected, but instead sends its own copy directly to all neighboring nodes, which itself remains on the current node. Therefore, there may be multiple viruses on a single node.
    • It is now known that hackers put a virus on the K nodes at the beginning.

For example, assume that a network connection is cut off after the school network is shown, consisting of 4 nodes and 4 links. At first there was only a virus on node 1.

At the beginning, Node 1 transmitted a virus to Node 2 and Node 3, which left 1 viruses on its own:

When one of the viruses reaches node 2, a virus is transmitted to Node 3. Another virus that reaches node 3 sends its own copy to Node 4:

When the virus that is transferred from Node 2 to Node 3 arrives, the virus sends a copy of itself to Node 4. At this point, there are 2 viruses left on node 3:

The last virus on each node is:

Little Hi and Little ho according to the current situation, after a period of time, all the node virus number will not change again. So how many viruses will there be in the end for the whole network?

Hint: application of topological sort

Input

Line 1th: 3 integers n,m,k,1≤k≤n≤100,000,1≤m≤500,000

Line 2nd: K integer a[i],a[i] means the hacker put 1 viruses on node a[i]. 1≤a[i]≤n

3rd.. M+2 line: 2 integer u,v per line, indicating that there is a network link from node u to node v. The data is guaranteed to be a loop-free diagram. 1≤u,v≤n

Output

Line 1th: An integer that represents the total number of viruses in the last network MOD 142857

Sample input
4 4 111 21 32 33 4
Sample output
6
"Ideas":

For a node I, if we can first calculate the number of the virus of all its precursor nodes, we can directly calculate the number of its last virus, but how to calculate all the precursor node?

This is going to start with the nature of the graph. Our current network is no ring, for any one node I, when it has all its own virus is transmitted out, its own number of viruses will not change. So we might as well start with a node without a precursor, which is 0.

for these nodes, it does not increase the number of viruses. Then we will distribute the virus according to the connection it is associated with, and then this node will not work. It might as well be deleted, and its associated side is also deleted, so that the graph will produce some new non-degree nodes. This keeps deleting points until all the points are deleted, and adding up the number of viruses for all points is the total number of viruses.

Code:

#include <bits/stdc++.h>using namespace std;const int n=1e5+10;const int Mod=142857;const double Eps=1e-8;const Double inf=1e10;int t,n,k,m,x;int father[n],v[n],indegree[n];vector <int >vec[n];bool topsort () {Queue<int &G    T;q;    while (!q.empty ()) Q.pop ();    for (int i=1; i<=n; i++) if (indegree[i]==0) Q.push (i);    int ans=0,sum=0;        while (!q.empty ()) {int U=q.front ();        Q.pop ();        sum++;        Ans+=v[u];        Ans%=mod;            for (int i=0; i<vec[u].size (); i++) {int temp=vec[u][i];            V[temp]+=v[u];            V[temp]%=mod;        if (--indegree[temp]==0) Q.push (temp);    }} printf ("%u\n", ans);    /* if (sum<n) return false; Determine topology sort return true;    */}int Main () {int u,v,a;        while (scanf ("%d%d%d", &n,&m,&k)!=eof) {memset (indegree,0,sizeof (Indegree));        for (int i=1;i<=n;i++) if (Vec[i].size ()) vec[i].clear (); for (int i=1; i<=k; i++) {scanf ("%d ", &v);        v[v]++;}            while (m--) {scanf ("%d%d", &v,&u);            Vec[v].push_back (U);        indegree[u]++;        }/* if (Topsort ()) puts ("YES");        Else puts ("NO");    */Topsort (); } return 0;}


Hihocoder #1175: Sort by topology • Two

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.