Topic on hiho topological sorting-48th and weeks, topic on hiho topological sorting

Source: Internet
Author: User

Topic on hiho topological sorting-48th and weeks, topic on hiho topological sorting

 

Topological sorting · 1

Analysis:

This is to determine whether a directed graph contains loops. If a ring exists, "Wrong" is output. If no ring exists, the course arrangement is reasonable and "Correct" is output ".

The prompt in the question is already very clear.

In general:

① Find a point with a 0 degree of access (indicating that there is no precursor to the point) and put the point into the set T. Delete all edges starting from the vertex;

② Traverse the remaining points, find the point with a 0 degree of access, and repeat the ① operation.

③ Until there is no point with an inbound degree of 0. End. If the set T contains all vertices, the graph does not have a ring. Otherwise, the graph has a ring.

Note: 1. When performing operation ①, When deleting an edge (u, v), update the inbound degrees of its related points (du [v] --);

2. When performing operation ②, You need to traverse all vertices. It is feasible when there are few vertices. If there are more vertices, it is easy to time out. Therefore, the prompt of the question shows a good way: execute Operation ① To update the inbound degree of the related vertex and directly judge whether it is 0. If it is 0, it is entered into the queue. This saves a lot of time.

For example:

The entry degree of start point 1 is 0, point 1 is added to the set T, and the edge starting from 1 is deleted; the entry degree of related points is updated, and the entry degree of points 2 and 3 is changed to 0, 2, 3 into the queue;

Perform the ① operation on vertex 2 and 3 in sequence, add T to vertex 2 and 3, delete edge (2, 4), and (3, 4). At this time, no other vertex enters 0, end operation. T does not contain all vertices, indicating that the graph has a ring;

 

 

# Include <iostream> # include <cstdio> # include <cstring> # include <string. h >#include <algorithm> # include <vector> # include <queue> using namespace std; int t, n, m, sum, du [100005]; vector <int> vec [100005]; int ac () {queue <int> q; for (int I = 1; I <= n; I ++) // traverse all vertices on one side and add all vertices with 0 inbound to queue q {if (du [I] = 0) q. push (I) ;}while (! Q. empty () {int tem = q. front (); // retrieve a vertex q with an inbound value of 0 in the queue. pop (); sum ++; // delete all edges (tem, v) starting from tem and update du [], for (int I = 0; I <vec [tem]. size (); I ++) {du [vec [tem] [I] --; if (du [vec [tem] [I] = 0) // if the value after the entry level of vec [tem] [I] is 0, the entry queue is q. push (vec [tem] [I]);} vec [tem]. clear () ;}if (sum = n) return 1; else return 0 ;}int main () {cin> t; while (t --) {scanf ("% d", & n, & m); // use vec [] to store an edge for (int I = 1; I <= n; I ++) vec [I]. clear (); memset (du, 0, sizeof (du); // initialization inbound, set to 0; for (int I = 1; I <= m; I ++) {int x, y; scanf ("% d", & x, & y); vec [x]. push_back (y); // Add the edge du [y] ++; // record inbound} sum = 0; int ans = ac (); if (ans = 1) printf ("Correct \ n"); else printf ("Wrong \ n");} return 0 ;}

 

Topological sorting · 2

Analysis:

Similar to the previous one, but the other is to record the number of viruses at each point.The number of viruses at each point = the number of viruses in itself + the sum of all the viruses at its nodes (Is the sum of the number of viruses at all its precursor points ).

# Include <iostream> # include <cstdio> # include <cstring> # include <string. h >#include <algorithm> # include <vector> # include <queue> using namespace std; const int mod = 142857; int n, m, k, sum, virus [100005], du [100005]; vector <int> vec [100005]; void ac () {queue <int> q; for (int I = 1; I <= n; I ++) {if (du [I] = 0) q. push (I) ;}while (! Q. empty () {int tmp = q. front (); q. pop (); sum = (sum + virus [tmp]) % mod; // Add the tmp virus count to the number of viruses at all the points whose precursor is tmp for (int I = 0; I <vec [tmp]. size (); I ++) {int B = vec [tmp] [I]; virus [B] = (virus [tmp] + virus [B]) % mod; // The modulo is also required here, du [B] --; if (du [B] = 0) q. push (B);} vec [tmp]. clear () ;}} int main () {while (scanf ("% d", & n, & m, & k )! = EOF) {memset (virus, 0, sizeof (virus); memset (du, 0, sizeof (du); for (int I = 1; I <= n; I ++) vec [I]. clear (); for (int I = 1; I <= k; I ++) {int x; scanf ("% d", & x ); virus [x] ++;} for (int I = 1; I <= m; I ++) {int x, y; scanf ("% d ", & x, & y); vec [x]. push_back (y); du [y] ++;} sum = 0; ac (); printf ("% d \ n", sum) ;}return 0 ;}

 

Related Article

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.