Skip to content

Instantly share code, notes, and snippets.

@kocko
Last active October 10, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kocko/b1204ca9d7fec5b2db5c1c208d461332 to your computer and use it in GitHub Desktop.
Save kocko/b1204ca9d7fec5b2db5c1c208d461332 to your computer and use it in GitHub Desktop.
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