[C language] brush LeetCode 207. Curriculum (M)

Now you need to choose the course a total of n, denoted by 0 to n-1.

Need some Advanced Placement courses before elective certain. For example, you want to study courses 0, you need to complete Lesson 1, we use a match to represent them: [0,1]

A given amount of courses and their prerequisites, determine whether it is possible to complete all courses of study?

Example 1:

Input: 2 [[1,0]] 
Output: true
explanation: There are two courses. Before learning Lesson 1, you need to complete the course 0. So it is possible.
Example 2:

Input: 2 [[1,0], [0,1]]
output: false
explanation: There are two courses. Before learning Lesson 1, you need to complete the course 0; 0 and before learning course, you should first complete the course 1. This is impossible.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/course-schedule
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

This question is difficult. Hard almost no contact with the drawing at work, so this question is to use the knowledge you adjacency matrix and the degree to will be dumbfounded.

Then you must also use an array simulation queue.

First two-dimensional array is defined to represent the graph is represented with a degree of each vertex of the directed graph, a one-dimensional array inDegree.

We start according to the input to create this directed graph, adjacency matrix manner, and the initialization of the array also good.

We then define an array simulate a queue of queues, all of the 0 degree point placed in the queue.

Then start queue traversal traverses from point connected graph, the arrival of each new node, which is a minus-degree, if at this time this point is 0-degree, into the end of the queue.

Until all of the values ​​in the queue been traversed, if the degree of the node at this time there is not 0, then the ring is present, it returns false, and vice versa returns true

int queue[10240]= {0};
int head = 0;
int tail = 0;

void queadd(int member) {
    queue[tail] = member;
    tail++;
}

int quepop() {
    int member = 0;
    member = queue[head];
    head++;

    return member;
}

void queinit() {
    memset(queue, 0, sizeof(int) * 10240);
    head = 0;
    tail = 0;
}
    
bool canFinish(int numCourses, int** prerequisites, int prerequisitesSize, int* prerequisitesColSize){
    int graph[numCourses][numCourses];
    int inDegree[numCourses];
    int i;
    int u;
    int num = 0;
    
    memset(graph, 0, sizeof(int) * numCourses * numCourses);
    memset(inDegree, 0, sizeof(int) * numCourses);
    
    queinit();
    
    for(i = 0; i < prerequisitesSize; i++) {
        graph[prerequisites[i][0]][prerequisites[i][1]] = 1;
        inDegree[prerequisites[i][1]]++;
    }
    
    for(i = 0; i < numCourses; i++) {
        if(inDegree[i] == 0){
            queadd(i);
        }
    }
    
    while(head < tail) {
        u = quepop();
        for(i = 0; i < numCourses; i++) {
            if(graph[u][i] != 0) {
                inDegree[i]--;
                if(inDegree[i] == 0){
                    queadd(i);
                }
            }
        }
        num++;
    }
    
    return (num == numCourses);    
}

 

发布了124 篇原创文章 · 获赞 17 · 访问量 11万+

Guess you like

Origin blog.csdn.net/jin615567975/article/details/104382277