problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::parallel_courses::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int minimumSemesters (int n, vector< vector< int > > &relations)
 

详细描述

在文件 leetcode.h3205 行定义.

成员函数说明

◆ minimumSemesters()

int leetcode::parallel_courses::Solution::minimumSemesters ( int  n,
vector< vector< int > > &  relations 
)
static

在文件 leetcode.cpp9115 行定义.

9115 {
9116 vector<node *> nodes(n);
9117 for(int i = 1; i <= n; i++) {
9118 nodes[i - 1] = new node(i, 0);
9119 }
9120 for(const auto &relation: relations) {
9121 nodes[relation[1] - 1]->pred.insert(nodes[relation[0] - 1]);
9122 nodes[relation[0] - 1]->out++;
9123 }
9124 bool flag = true;
9125 unordered_set<int> vis;
9126 while(flag) {
9127 flag = false;
9128 for(int i = 0; i < n; i++) {
9129 if(nodes[i]->out == 0) {
9130 if(vis.contains(i)) {
9131 continue;
9132 }
9133 vis.insert(i);
9134 flag = true;
9135 for(node *next: nodes[i]->pred) {
9136 next->out--;
9137 next->len = max(next->len, nodes[i]->len + 1);
9138 }
9139 }
9140 }
9141 }
9142 int ans = 0;
9143 for(int i = 0; i < n; i++) {
9144 if(nodes[i]->out != 0) {
9145 return -1;
9146 }
9147 ans = max(ans, nodes[i]->len);
9148 }
9149 return ans;
9150 }

被这些函数引用 leetcode::parallel_courses::TEST().


该类的文档由以下文件生成: