-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkotlin.kt
61 lines (54 loc) · 1.82 KB
/
kotlin.kt
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*
class Edge(val from: Int, val to: Int, val capacity: Int)
fun minCut(n: Int, edges: List<Edge>, source: Int, sink: Int): Set<Int> {
val graph = Array(n) { mutableListOf<Int>() }
val capacities = Array(n) { IntArray(n) }
val flow = IntArray(n)
val seen = BooleanArray(n)
edges.forEach {
graph[it.from].add(it.to)
capacities[it.from][it.to] = it.capacity
}
// Use BFS to find a path from the source to the sink
val queue = ArrayDeque<Int>()
queue.add(source)
flow[source] = Int.MAX_VALUE
while (queue.isNotEmpty()) {
val from = queue.poll()
seen[from] = true
graph[from].forEach { to ->
if (!seen[to] && capacities[from][to] > 0) {
queue.add(to)
flow[to] = Math.min(flow[from], capacities[from][to])
}
}
}
// If no path from source to sink was found, return an empty set
if (!seen[sink]) {
return emptySet()
}
// Use the path found to update capacities and add edges to the residual graph
val residualGraph = Array(n) { mutableListOf<Int>() }
var from = source
while (from != sink) {
val to = graph[from].first { seen[it] }
capacities[from][to] -= flow[sink]
capacities[to][from] += flow[sink]
residualGraph[to].add(from)
residualGraph[from].add(to)
from = to
}
// Find the connected component of the source in the residual graph
val component = BooleanArray(n)
fun dfs(i: Int) {
component[i] = true
residualGraph[i].forEach { j ->
if (!component[j]) {
dfs(j)
}
}
}
dfs(source)
// Return the set of nodes in the connected component
return component.withIndex().filter { it.value }.map { it.index }.toSet()
}