-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
47 lines (46 loc) · 1.37 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
namespace ParallelCourses
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
public int MinimumSemesters(int n, int[][] relations)
{
int[] inDegree = new int[n + 1];
var graph = new List<int>[n + 1];
for (int i = 1; i < graph.Length; i++)
graph[i] = new List<int>();
foreach (var relation in relations)
{
graph[relation[0]].Add(relation[1]);
inDegree[relation[1]]++;
}
var queue = new Queue<int[]>();
for (int i = 1; i < inDegree.Length; i++)
{
if (inDegree[i] != 0) continue;
queue.Enqueue(new[] { i, 1 });
n--;
}
int res = 0;
while (queue.Count != 0)
{
var cur = queue.Dequeue();
res = Math.Max(res, cur[1]);
foreach (var next in graph[cur[0]])
{
inDegree[next]--;
if (inDegree[next] != 0) continue;
queue.Enqueue(new[] { next, cur[1] + 1 });
n--;
}
}
return n == 0 ? res : -1;
}
}
}