Hiho the 48th Zhou topology sort • Two "application of topological sorting + time optimization of static array + topological sorting algorithm"

Source: Internet
Author: User

Topic 1: Topology sequencing • Two time limit:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

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 

algorithm analysis: This is a topological sort of application topic, because the amount of data is very large. So DFS burst is bound to time out, thinking carefully can be done by topological sort. The
idea is: Set a cnt[] array, cnt[i] means: I node has cnt[i] virus. Assume that the node that is currently accessed is is the cur,cur node has cnt[cur]
virus, and before we remove it, copy all of that point's virus to the node it points to.

Note here: Each time in the calculation of a node has how many viruses, the process of calculation to take the remainder of the operation, because the number of points is very large, constant accumulation of the type overflow.
There is one more place to note, I use my previous general topology sort to write the topology sort, each time to do in[] (into the degree group) traversal Loop, find a
a 0 in the node and then remove the operation, each time it is really time to traverse. This is written after I submit the code tle, got 70 points. In general it seems that the process of the algorithm is for the
, but the time needs to be optimized. So I thought, since every time the node to find the degree of 0 to go through the array, then do not traverse will save time. So I think of the
can use a queue to save all the current graph in the 0 of the node, one at a time to take the action on the line, a decisive AC.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <string> #include <stack> #include <vector> #include <set> #include <queue> #include <algorithm> #define N 100000+10//maximum number of nodes # define M 500000+10//maximum number of edges # Define MOD 142857using namespace Std;int N, M, K;int Cnt[n]; Record the number of viruses per node int edgecnt; int head[n]={0}; The head node array is as large as the number of nodes int p[m]; Point to array and number of sides as large as int next[m]={0};    Analog pointer, initialized to 0int in[n];void addedge (int u, int v) {++edgecnt; P[edgecnt]=v;    The direction of the current edge is v next[edgecnt]=head[u]; head[u]=edgecnt;}    int topsort_cal () {Queue<int>q;while (!q.empty ()) Q.pop ();    int I, J;    int ans=0;    for (I=1; i<=n; i++)//order to traverse once in[] initialize the queue if (in[i]==0) Q.push (i);    int cur; while (!q.empty ())//queue is not empty and there is a node with a degree of 0 {cur=q.front (); Q.pop ();//Don't forget to queue ans = (ans%mod + cnt[cur]%mod)%mod        ;        in[cur]--; for (J=head[cur]; j; j=next[j]) {in[p[j]]--;                if (in[p[j]]==0)//delete an edge pointing to that point to determine if the point is now in the degree of 0? Q.push (P[j]);                                                            If yes then go to queue cnt[p[j]]= (Cnt[p[j]]%mod + cnt[cur]%mod)%mod;//If you do not take redundancy here To the back will overflow}} return ans;    int main () {int i, J;    scanf ("%d%d%d", &n, &m, &k);    memset (CNT, 0, sizeof (CNT));    int DD;        for (i=0; i<k; i++) {scanf ("%d", &AMP;DD); cnt[dd]++;    Place virus} int u, v;    edgecnt=0; memset (in, 0, sizeof (in));        Into the degrees group empty for (i=0; i<m; i++) {scanf ("%d%d", &u, &v); Addedge (U, v);    Establish one-way edge in[v]++;    } int ans=topsort_cal ();    printf ("%d\n", ans); return 0;}

Hiho the 48th Zhou topology sort • Two "application of topological sorting + time optimization of static array + topological sorting algorithm"

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.