#1175: Topology sequencing • Two 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
There is a place to take the mold, pay attention to all operations modulo, to prevent the bug ...
AC Code:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace std; #define MOD 142857int N, m, k;const int maxn = 100005;vector<int> g[maxn];i NT Indeg[maxn];int sum[maxn];int BFS () {int ret = 0;queue<int> que;for (int i = 1; I <= n; i + +) {if (indeg[i] = = 0 {Que.push (i); ret = (ret + sum[i])% MOD;}} while (!que.empty ()) {int u = que.front (), Que.pop (), int d = g[u].size (), for (int i = 0; i < D; i + +) {int v = g[u][i];ind EG[V]--;sum[v] = (Sum[v] + sum[u])% mod;//Remember to mold the MOD, or it will overflow. if (indeg[v] = = 0) {Que.push (v); ret = (ret + sum[v])% MOD;}} return ret;} int main () {while (scanf ("%d%d%d", &n, &m, &k)! = EOF) {for (int i = 1; I <= n; i + +) g[i].clear (); MEMSET (sum, 0, sizeof (sum)), memset (indeg, 0, sizeof (INDEG)), for (int i = 0; i < K; i + +) {int x;scanf ("%d", &x); Sum[x] + +;} for (int i = 0; i < m; i + +) {int u, v;scanf ("%d%d", &u, &v); G[u].push_back (v); Indeg[v] + +;} printf ("%d\n", BFS ());} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hihoCoder-1175-Topology sequencing • Two (application of topological sequencing)