-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
34 lines (28 loc) · 892 Bytes
/
Solution.cs
File metadata and controls
34 lines (28 loc) · 892 Bytes
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
public class Solution
{
public int MinReorder(int n, int[][] connections)
{
int count = 0;
bool[] mapped = new bool[n];
Dictionary<int, List<(int city, bool direction)>> graph = [];
foreach (var connection in connections)
{
graph[connection[0]] = graph.GetValueOrDefault(connection[0], []);
graph[connection[0]].Add((connection[1], true));
graph[connection[1]] = graph.GetValueOrDefault(connection[1], []);
graph[connection[1]].Add((connection[0], false));
}
void DFS(int city)
{
mapped[city] = true;
foreach (var (node, direction) in graph[city])
{
if (mapped[node]) continue;
if (direction) count++;
DFS(node);
}
}
DFS(0);
return count;
}
}