"Leetcode 207" Course Schedule

Source: Internet
Author: User
Tags in degrees

There is a total of n courses you have to take, labeled from 0 to n - 1 .

Some courses May has prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a Pair[0,1]

Given the total number of courses and a list of prerequisite pairs, are it possible for your to finish all courses?

For example:

2, [[1,0]]

There is a total of 2 courses to take. To take course 1 should has finished course 0. So it is possible.

2, [[1,0],[0,1]]

There is a total of 2 courses to take. To take course 1 should has finished course 0, and to take course 0 you should also has finished course 1. So it is impossible.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices.

Ideas:

This topic can be abstracted as a loop detection problem in a forward graph. The conventional solution is to detect if there is a loop in the direction graph by topological sort after constructing adjacency matrix or adjacency linked list , if there is a loop to explain the child's tragedy is about to face the crisis of dropping out, return false, otherwise, Children can choose their own classes until graduation (but in fact, even if they can graduate smoothly, there is no * use).

Here I use two sets of container arrays to build the diagram (considering that there may be repeated input and so on, there is also lazy--!). ), and then the topology is sorted to obtain the result.

1 classSolution {2  Public:3     BOOLCanfinish (intNumcourses, vector<pair<int,int> >&Prerequisites) {4 5         Const intn =numcourses;6         intCNT =0;7 8         Set<int>Sets[n], grap[n];9queue<int> Queue;//queues, topology ordering prerequisitesTen  One         //build a diagram from a given input, Sets[i] represents a collection of post-curriculum courses that can be learned after learning lesson I A         //Grap[i] Represents the set of pre-courses required to study course I (in degrees) -vector<pair<int,int> >::iterator it =Prerequisites.begin (); -          for(; it! = Prerequisites.end (); it++) the         { -sets[(*it). Second].insert (*it). first); -grap[(*it). First].insert (*it). second); -         } +  -         //pressing a course that does not require a predecessor course (a course with a 0 degree) into the queue +          for(inti =0; I < n; i++) A             if(grap[i].size () = =0) at Queue.push (i); -  -          while(!queue.empty ()) -         { -             //Take out the team's first class -             intCur =Queue.front (); in Queue.pop (); -Traverse the current course to             //traverse the post-course of the current course (cur), removing the current course from the set of pre-courses (Grap[*it]) in the post-curriculum (*it) +             //If the course's pre-set of courses is empty after removing the current course, press it into the queue -             Set<int>::iterator it =Sets[cur].begin (); the              for(; it! = Sets[cur].end (); it++) *             { $grap[*it].erase (cur);Panax Notoginseng                 if(grap[*it].size () = =0) -Queue.push (*it); the             } +cnt++;//statistics total topology out of how many courses A         } the  +         //if the number of courses in the topology is equal to the total number of courses, no loop, return true, reverse, return false -         returnCNT = = n?1:0; $     } $};

"Leetcode 207" Course Schedule

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.