【Topological Sorting Topic】 hihocoder 1175 Topological Sorting · Two

https://hihocoder.com/problemset/problem/1175

# 1175: Topological sorting · Two

Time limit: 10000ms

Single point time limit: 1000ms

Memory limit: 256MB

description

The campus network of the school where Xiao Hi and Xiao Ho are located was hacked and a virus was dropped. This matter immediately aroused everyone's discussion on the school BBS, of course, Xiao Hi and Xiao Ho also participated in it. From what everyone knows, Xiao Hi and Xiao Ho have compiled the following information:

  • The backbone of the campus network is composed of N nodes (number 1..N), and there are some unidirectional network connections between these nodes. If there is a network connection (u, v) linking node u and node v, node u can send information to node v, but node v cannot send information to node u through the link.
  • When the virus was just infected, the campus network immediately cut off some network links, which just made the remaining network connections without rings, avoiding repeated infections of the nodes. In other words, the virus spreading from node i will never return to node i.
  • When a virus infects a node, it does not check whether the node is infected, but directly sends a copy of itself to all neighbor nodes, and it will stay on the current node itself. So there may be multiple viruses on one node.
  • It is now known that hackers put a virus on K nodes at the beginning.

For example, suppose that the school network is composed of 4 nodes and 4 links as shown in the figure below after cutting off some network connections. Initially, only node 1 had a virus.

Initially, node 1 transmitted a virus to node 2 and node 3, leaving 1 virus:

After one of the viruses reached node 2, a virus was transmitted to node 3. Another virus that reaches node 3 sends its own copy to node 4:

When the virus transmitted from node 2 to node 3 arrives, the virus sends a copy of itself to node 4. At this time, there are 2 viruses left on node 3:

The final virus on each node is:

According to the current situation, Little Hi and Little Ho discovered that after a period of time, the number of viruses on all nodes will definitely not change. So for the entire network, how many viruses will there be in the end?

Tip: Application of topological sorting

Input

Line 1: 3 integers N, M, K, 1≤K≤N≤100,000, 1≤M≤500,000

Line 2: K integers A [i], A [i] means that the hacker put a virus on node A [i]. 1≤A [i] ≤N

Line 3: M + 2: 2 integers u, v per line, indicating that there is a network link from node u to node v. The data is guaranteed to be an acyclic graph. 1≤u, v≤N

Output

Line 1: An integer representing the number of viruses in the entire network at the end MOD 142857

Sample input

4 4 1
1
1 2
1 3
2 3
3 4

Sample output

6

AC Code

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector> 
#include <queue>
using namespace std;
const int nmax=1e5+10;
int inDegree [nmax]; // Save the in-degree of each node
 int virus [nmax]; // Record the virus number of each node 
 vector <int> G [nmax]; // Adjacency list 
const int mod=142857;
 int n, m, k; // Number of points, number of edges 

void toposort(){
	queue<int>q;
	 while (! q.empty ()) q.pop (); // Empty the queue
	 for (int i = 1; i <= n; i ++) {// Vertex number: 1 ~ N
		if(inDegree[i]==0){
			 q.push (i); // Throw vertices with a degree of 0 into the queue 
		} 
	} 
	while(!q.empty()){
		int u=q.front();
		 q.pop (); // Delete the point
		 for (int i = 0; i <G [u] .size (); i ++) {// Traverse all edges adjacent to point u and delete it. 
			int v=G[u][i];
			inDegree[v]--;
			if(inDegree[v]==0){
				q.push(v);
			} 
			virus[v]=(virus[v]+virus[u])%mod; 
		} 
		G[u].clear();
	}
}
int main(int argc, char** argv) {
	while(scanf("%d %d %d",&n,&m,&k)!=EOF){
		memset(inDegree,0,sizeof(inDegree));
		memset(virus,0,sizeof(virus));
		 for (int i = 1; i <= n; i ++) {// Initialize and clear the adjacency list 
			G[i].clear();
		}
		while(k--){
			int x;
			scanf("%d",&x);
			 virus [x] ++; // Place the initialization virus 
		}
		int u,v;
		for(int i=0;i<m;i++){
			scanf("%d %d",&u,&v);
			G[u].push_back(v);
			inDegree[v]++; 
		} 
		toposort();
		int ans=0;
		for(int i=1;i<=n;i++){
			ans=(ans+virus[i])%mod;
		} 
		printf("%d\n",ans);
	} 
	return 0;
}

 

Intelligent Recommendation

[1174] topological sorting topic topological sorting hihocoder (judgment ring)

https://hihocoder.com/problemset/problem/1174 # 1174: a topological sorting · Time limit: 10000ms A single point of time: 1000ms Memory Limit: 256MB description As today's class teacher of spec...

hihoCoder #1175: Topological Sort · Two (Topological Sort)

Title: Chinese title. Hey. (I actually want to post a link to a topic every time I write a blog, but I forgot about it every time >.<), just give you n points and m edges (DAG), he will put the ...

hihoCoder # 1174 a topological sorting ·

                            hihoCoder # 1174 a topological sorting · answer: Bare title topological sort, since the su...

hihoCoder 1174 a topological sorting ·

# 1174: a topological sorting · Time limit: 10000ms A single point of time: 1000ms Memory Limit: 256MB description As today's class teacher of special boring, small and small Hi Ho secretly tal...

hihoCoder # 1174: a topological sorting ·

description As today's class teacher of special boring, small and small Hi Ho secretly talking. Small Ho: Small Hi, this semester you have what lessons it? Small Hi: a lot of, such as XXX1, XXX2 there...

More Recommendation

hihocoder 1175: Topological sorting·Two

Title link:http://hihocoder.com/problemset/problem/1175 time limit:10000ms Single point time limit:1000ms Memory limit:256MB description The campus network of the school where Xiao Hi and Xiao Ho are ...

hihoCoder 1175 topological sort

hihoCoder 1175 topological sort Title link: https://hihocoder.com/problemset/problem/1175 1. Topic analysis This question is a typical topological sorting question. This question can be solved either ...

[HihoCoder 1175 Topological Sorting·Two]

description hihocoder 1175 Ideas answer...

HihoCoder-1175 Topological Sort · Two (Topological Sort, Ideas)

description The campus network of the school where Xiao Hi and Xiao Ho are located was hacked and put a virus. This matter immediately aroused everyone's discussion on the school's BBS. Of course, Xia...

hihocoder 1174 Topological Sorting·One (Topological Sorting)

Title link:http://hihocoder.com/problemset/problem/1174 #1174: Topological sorting·One time limit:10000ms Single point time limit:1000ms Memory limit:256MB description Because the teacher in cl...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top