-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds gauss xor and it's respective tests
- Loading branch information
Showing
9 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const int MAXXI = 2009; | ||
using Equation = bitset<MAXXI>; | ||
struct GaussXor { | ||
vector<char> B; | ||
vector<Equation> A; | ||
|
||
void add(const Equation& ai, bool bi) { | ||
A.push_back(ai); | ||
B.push_back(bi); | ||
} | ||
|
||
pair<bool, Equation> solution() { | ||
int cnt = 0, n = A.size(); | ||
Equation vis; vis.set(); | ||
Equation x; | ||
for(int j = MAXXI-1, i; j >= 0; j--) { | ||
for(i = cnt; i < n; i++) { | ||
if (A[i][j]) break; | ||
} | ||
if (i == n) continue; | ||
swap(A[i], A[cnt]), swap(B[i], B[cnt]); | ||
i = cnt++; vis[j] = 0; | ||
for(int k = 0; k < n; k++) { | ||
if (i == k || !A[k][j]) continue; | ||
A[k] ^= A[i]; B[k] ^= B[i]; | ||
} | ||
} | ||
x = vis; | ||
for(int i = 0; i < n; i++) { | ||
int acum = 0; | ||
for(int j = 0; j < MAXXI; j++) { | ||
if (!A[i][j]) continue; | ||
if (!vis[j]) { | ||
vis[j] = 1; | ||
x[j] = acum^B[i]; | ||
} | ||
acum ^= x[j]; | ||
} | ||
if (acum != B[i]) return {false, Equation()}; | ||
} | ||
return {true, x}; | ||
} | ||
|
||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
\subsection{Gauss XOR Elimination / XOR-SAT} | ||
|
||
Execute gaussian elimination with xor over the system $Ax = b$ in. The add method must receive a bitset indicating which variables are present in the equation, and the solution of the equation. | ||
|
||
Time complexitiy: $O(\frac{nm^{2}}{64})$ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
4 4 | ||
4 2 | ||
1 3 | ||
2 3 | ||
1 4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
4 3 | ||
4 2 | ||
2 3 | ||
1 2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
5 5 | ||
3 5 | ||
3 1 | ||
1 4 | ||
2 5 | ||
2 4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#include "../../../../algorithms/math/gauss-xor.cpp" | ||
|
||
int32_t main() { | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
int n, f; | ||
cin >> n >> f; | ||
|
||
vector<vector<int>> adj(n); | ||
while (f--) { | ||
int u, v; | ||
cin >> u >> v; | ||
u--, v--; | ||
adj[u].push_back(v); | ||
adj[v].push_back(u); | ||
} | ||
|
||
GaussXor gauss; | ||
for (int i = 0; i < n; i++) { | ||
Equation eq; | ||
|
||
for (auto v : adj[i]) { | ||
eq.set(v); | ||
} | ||
|
||
if (adj[i].size() & 1) { | ||
eq.set(i); | ||
gauss.add(eq, 0); | ||
} | ||
else { | ||
gauss.add(eq, 1); | ||
} | ||
} | ||
|
||
cout << "NY\n"[gauss.solution().first] << '\n'; | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Y |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Y |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
N |