Last active
October 10, 2018 13:25
-
-
Save kocko/b1204ca9d7fec5b2db5c1c208d461332 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void buildGraph() { | |
int n = grid.length; | |
for (int i = 0; i < n; i++) { | |
for (int j = i + 1; j < n; j++) { | |
if (grid[i][j] == -1 || grid[j][i] == -1) continue; | |
if (grid[i][j] == grid[j][i]) { | |
if (grid[i][j] == 0) { | |
imply(2 * i, not(2 * j)); | |
imply(2 * j, not(2 * i)); | |
} else { | |
imply(not(2 * i), 2 * j); | |
imply(not(2 * j), 2 * i); | |
} | |
} else if (grid[i][j] == 0) { | |
imply(2 * i, 2 * j); | |
} else { | |
imply(2 * j, 2 * i); | |
} | |
} | |
} | |
} | |
void imply(int from, int to) { | |
graph.get(from).add(to); | |
reverse.get(to).add(from); | |
} | |
int not(int value) { | |
return value ^ 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment