-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_1466.cs
47 lines (40 loc) · 1.45 KB
/
No_1466.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.Numerics;
namespace LeetCode
{
class No_1466
{
public int MinReorder(int n, int[][] connections)
{
List<int> visited = new List<int>();
Queue<int> check = new Queue<int>();
Dictionary<int, List<(int, int)>> matriz = new Dictionary<int, List<(int, int)>>();
int output = 0;
check.Enqueue(0);
for (int i = 0; i < connections.Length; i++)
{
if(!matriz.ContainsKey(connections[i][0]))
matriz[connections[i][0]] = new List<(int, int)>();
matriz[connections[i][0]].Add((connections[i][1], 0));
if(!matriz.ContainsKey(connections[i][1]))
matriz[connections[i][1]] = new List<(int, int)>();
matriz[connections[i][1]].Add((connections[i][0], 1));
}
while (check.Count != 0)
{
int cur = check.Dequeue();
if (!visited.Contains(cur))
{
visited.Add(cur);
for (int i = 0; i < matriz[cur].Count; i++)
{
if(!visited.Contains(matriz[cur][i].Item1)){
check.Enqueue(matriz[cur][i].Item1);
output += matriz[cur][i].Item2;
}
}
}
}
return output;
}
}
}