-
Notifications
You must be signed in to change notification settings - Fork 7
/
1274 The Perfect Stall.java
45 lines (44 loc) · 1.29 KB
/
1274 The Perfect Stall.java
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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int cowNumber = in.nextInt();
int stallNumber = in.nextInt();
boolean[][] map = new boolean[cowNumber + 1][stallNumber + 1];
int[] linker = new int[stallNumber + 1];
Arrays.fill(linker, -1);
for (int i = 1; i <= cowNumber; ++i) {
int preferNumber = in.nextInt();
for (int j = 0; j < preferNumber; ++j) {
int prefer = in.nextInt();
map[i][prefer] = true;
}
}
System.out.println(hungarian(map, linker, cowNumber, stallNumber));
}
}
private static int hungarian(boolean[][] map, int[] linker, int cowNumber, int stallNumber) {
int rst = 0;
for (int i = 1; i <= cowNumber; ++i) {
boolean[] used = new boolean[stallNumber + 1];
if (dfs(map, linker, used, cowNumber, stallNumber, i)) {
rst++;
}
}
return rst;
}
private static boolean dfs(boolean[][] map, int[] linker, boolean[] used, int cowNumber, int stallNumber, int cow) {
for (int i = 1; i <= stallNumber; ++i) {
if (map[cow][i] && !used[i]) {
used[i] = true;
if (linker[i] == -1 || dfs(map, linker, used, cowNumber, stallNumber, linker[i])) {
linker[i] = cow;
return true;
}
}
}
return false;
}
}