Hiho the 47th Zhou flutter sort One "static array chain-forward star storage structure implementation + topology Jump"

Source: Internet
Author: User

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

As the teacher in class today is particularly boring, little hi and small ho secretly chat up.

Little ho: Little Hi, do you have any classes to choose this semester?

Small hi: Quite a lot, such as xxx1,xxx2 and XXX3. Originally wanted to choose YYY2, but did not seem to choose YYY1 First, can not choose YYY2.

Little ho: The first course is a real hassle.

Little hi: Yes. Many courses have first-class courses, and each time you choose a course, you have to check first. The academic records of the first course published are many years ago, not only have duplicate information, it seems that many are not correct.

Little ho: There are too many courses, and the Dean can't tidy it up. They couldn't make a single confirmation that there was a mistake.

Little hi: This is not the time for you to be a little ho!

Little ho: eh??

We all know that college courses can be chosen by themselves, and each semester is free to choose which courses to study. The only restriction is that we have a sequential relationship between courses: some of the most difficult courses may require some pre-set courses. For example, course A is a pre-set course for Course B and requires a course to be completed before a B course can be selected. The university's dean collects the sequential relationships of all courses, but there may be some information errors due to system failures. Now little Ho will tell you the information, please help small ho to determine whether the information is wrong. The wrong information mainly refers to the situation where "course a is a pre-course for Course B, and course B is a pre-curriculum for course a". Course A is a pre-set course for Course B, course B is a pre-curriculum for Course C, and course C is a pre-curriculum for course A, which is also wrong.

Hint: Topology ordering

Input

Line 1th: An integer t that represents the number of groups of data T (1 <= t <= 5)
Next the T Group data is in the following format:
Line 1th: 2 integers, n,m. n indicates the total number of courses and the course number is 1. N. M represents the number of sequential relationships. 1 <= N <= 100,000. 1 <= M <= 500,000
2nd.. M+1 line: 2 integers per line, a, B. Indicates that course a is a pre-set course for Course B.

Output

1th.. T line: 1 strings per line, if the group information is correct, output "Correct", if the group information is wrong, output "wrong".

Sample input
22 21 22 13 21 21 3
Sample output
 wrongcorrect 

algorithm analysis: The idea of topological sequencing is very easy to implement code, this does not describe the implementation of topological sequencing. This topic has a large amount of data and needs to be stored using a static array or a vector to emulate the adjacency table
. It is important to note that in the process of topology, if one traversal in[] into the degree group does not find a node in the degree of 0, the process of jumping out of the topology, do not
continue the topology, continue the topology is a waste of time, because the remaining points are in the ring. If you do not jump, the submission will be timed out, only 60 points.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #define MAXN 100000+10#define maxm 500000+10using namespace std;int N, m;int head[MAXN + 1] = {0};//represents the head pointer, initialized to 0int p[MAXM + 1];//represents the node that points to int next[MAXM + 1] = {0}; The analog pointer, initialized to the number of 0int edgecnt;//record edges int in[maxn];void Addedge (int u, int v) {//Add Edge (u,v) ++edgecnt;p[edgecnt] = V;//Indicates the current edge refers to The point is vnext[edgecnt] = head[u];head[u] = edgecnt;}    BOOL Topsort () {int cnt=n;//number of nodes int cc=0;    BOOL Flag;        while (cnt--) {flag=false; for (int i=1; i<=n; i++) {if (in[i]==0) {in[i]--;//flag=t                Rue cc++;                    Found a new node in the degree of 0 for (int j=head[i]; j; j=next[j]) {int v=p[j];                in[v]--;    }}} if (Flag==false) break;    } if (CC < n) return false; else return true;} int main () {int t;sCANF ("%d", &t); int I, J, K;int U, v;while (t--) {scanf ("%d%d", &n, &m), memset (in, 0, sizeof (in)); Memset (Head, 0 , sizeof (head)), memset (next, 0, sizeof (next)), Edgecnt=0;for (i=0; i<m; i++) {scanf ("%d%d", &u, &v);//u-> Vaddedge (U, v);  Establish one-way edge only one time//otherwise Addedge (V, u); in[v]++;        The node is pointed to the degree plus 1}if (topsort ()) printf ("correct\n"); else printf ("wrong\n");} return 0;}

Hiho the 47th Zhou flutter sort One "static array chain-forward star storage structure implementation + topology Jump"

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.