- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatching.java
More file actions
Latest commit
52 lines (44 loc) · 1.45 KB
/
Copy pathMatching.java
File metadata and controls
52 lines (44 loc) · 1.45 KB
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
importjava.util.*;
importjava.io.*;
publicclassMatching {
publicstaticfinalintmod = 1000000007;
publicstaticintcount(intk) { //count number of ones in binary
intc = 0;
while (k > 0) {
k &= k-1;
c++;
}
returnc;
}
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
intn = Integer.parseInt(br.readLine());
String[] s;
boolean[][] matches = newboolean[n][n];
for (inti = 0; i < n; i++) {
s = br.readLine().split(" ");
for (intj = 0; j < n; j++)
matches[i][j] = Integer.parseInt(s[j]) == 1;
}
int[] dp = newint[(1 << n)];
dp[0] = 1;
for (intj = 0; j < (1 << n) - 1; j++) {
inttaken = count(j); //number of women left
for (inti = 0; i < n; i++) { //left therefore goes from n toward zero
// if (matches[taken][i])
// System.out.println("1*");
// else
// System.out.println("0*");
if (matches[taken][i] && !((j & (1<<i)) == 1)) { //1 means taken, 0 means available
//if man [i] can be matched with woman [#remaining]
//and this woman has not already been taken
dp[j^(1<<i)] = (dp[j^(1<<i)] + dp[j]) % mod;
System.out.printf("%-5s %d %d %s\n", Integer.toBinaryString(j), taken, i, Integer.toBinaryString(j^(1<<i)) );
//then add onto the case where this woman is taken (XOR)
//the number of possibilities if she had not
}
}
}
System.out.println(dp[(1<<n)-1]);
}
}