Hiho click & #183; 47 topological sorting & #183; 1 (determine whether Topo sorting can be performed), hihotopo

Source: Internet
Author: User

Hiho-47 topological sorting-1 (determining whether Topo can be sorted), hihotopo

Meaning Chinese

Simple Topo sorting: it is convenient to use vector to implement the adjacent table.

#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 5;vector<int> e[N];vector<int>::iterator it;int n, m, ideg[N];bool topo(){    int cur;    queue<int> q;    for(int i = 1; i <= n; ++i)        if(!ideg[i]) q.push(i);    while(!q.empty())    {        cur = q.front();        q.pop();        for(it = e[cur].begin(); it != e[cur].end(); ++it)        {            if(!ideg[*it]) continue;            --ideg[*it];            if(!ideg[*it]) q.push(*it);        }    }    for(int i = 1; i <= n; ++i)        if(ideg[i]) return false;    return true;}int main(){    int cas, a, b;    scanf("%d", &cas);    while(cas--)    {        memset(ideg, 0, sizeof(ideg));        scanf("%d%d", &n, &m);        for(int i = 1; i <= n; ++i) e[i].clear();        for(int i = 0; i < m; ++i)        {            scanf("%d%d", &a, &b);            e[a].push_back(b);            ++ideg[b];        }        puts(topo() ? "Correct" : "Wrong");    }    return 0;}//Last modified :   2015-05-25 09:05

Topology Sorting-time limit: 256 ms single-point time limit: Ms memory limit: MB
Description

Because the teacher in the class spoke very boring today, Xiao Hi and Xiao Ho chatted secretly.

Ho: Hi, do you have any courses this semester?

Small Hi: there are many, such as XXX1, XXX2, and XXX3. If you want to select YYY2, but it seems that you have not selected YYY1 first, you cannot select YYY2.

Small Ho: It's really troublesome to take a course first.

Hi, that's right. Many of the courses are offered in advance. You have to check whether there is any advance course before each course selection. The academic records for predicate Courses published by the academic administration were recorded many years ago. Not only do they have duplicate information, but it seems that many of them are incorrect.

Small Ho: there are too many courses, so you can't sort them out. They cannot confirm whether there are any mistakes one by one.

Xiao Hi: Isn't it the time for Xiao Ho to get out of the game!

Small Ho: ah ??

We all know that university courses can be selected by ourselves, and each semester is free to choose the courses we plan to study. The only restriction on course selection is the sequential relationship between some courses: some difficult courses may have requirements for some pre-courses. For example, if course A is A pre-course of course B, you must finish course A before selecting course B. The Academic Affairs of the university collect the sequence of all courses, but some information may be wrong due to system faults. Now, Xiao Ho tells you all the information. Please help Xiao Ho determine whether the information is correct. The incorrect information mainly refers to the situation where "course A is the prepaid course of course B, and course B is also the prepaid course of course. Of course, "course A is A pre-course of course B, course B is A pre-course of course C, and course C is A pre-course of course A" is also wrong.

Tip: topological sorting

Input

Row 1st: 1 integer T, indicating the number of data groups T (1 <= T <= 5)
The T group data follows the following format:
Row 1st: two integers, N, and M. N indicates the total number of courses. The number of courses is 1. N. M indicates the number of sequential links. 1 <= N <= 100,000. 1 <= M <= 500,000
2nd. M + 1 rows: 2 integers in each row, A and B. It indicates that course A is A pre-course of course B.

Output

Row 1st. T: one character string per line. If the group information is Correct, "Correct" is output. If the group information is incorrect, "Wrong" is output ".

Sample Input
22 21 22 13 21 21 3
Sample output
WrongCorrect

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.