Zoj 3656 bit magic (2-Sat judgment)

Source: Internet
Author: User

[Question link]

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3656.

[Topic]

Assume that an array a [n] is known, use the following function to generate a matrix B:

void calculate(int a[N], int b[N][N]) {for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {if (i == j) b[i][j] = 0;else if (i % 2 == 1 && j % 2 == 1) b[i][j] = a[i] | a[j];else if (i % 2 == 0 && j % 2 == 0) b[i][j] = a[i] & a[j];else b[i][j] = a[i] ^ a[j];}}}
If a matrix B is known, can array a be converted to matrix B?


[Analysis]

This is actually an enhanced version of poj 3678 Katu puzzle.

Poj3678 each number can only be 0 or 1, equivalent to a binary integer with only one digit.

The range of each number in this question is 0 ≤ B [I] [J] ≤ 2 ^ 31-1, which is equivalent to a 31-bit integer in the binary system, perform a 2-Sat judgment on each binary!

[Code]

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long int64; const int maxn = 610; const int VN = maxn * 62; const int en = 2000010; int N; int mat [maxn] [maxn]; struct graph {int size; int head [VN]; struct edge {int V, next ;} E [En]; void Init () {size = 0; memset (Head,-1, sizeof (head);} void addedge (int u, int V) {e [size]. V = V; E [size]. next = head [u]; head [u] = size ++ ;}} G; Class two_sat {public: bool check (const graph & G, const int N) {SCC (G, n); For (INT I = 0; I <n; ++ I) if (belong [I * 2] = belong [I * 2 + 1]) return false; return true;} PRIVATE: void Tarjan (const graph & G, const int U) {int V; dfn [u] = low [u] = ++ idx; STA [top ++] = u; instack [u] = true; For (int e = G. head [u]; e! =-1; E = G. E [e]. next) {v = G. E [e]. v; If (dfn [v] =-1) {Tarjan (G, V); low [u] = min (low [u], low [v]);} else if (instack [v]) {LOW [u] = min (low [u], dfn [v]);} if (low [u] = dfn [u]) {++ bcnt; do {v = sta [-- top]; instack [v] = false; belong [v] = bcnt;} while (u! = V) ;}} void SCC (const graph & G, const int N) {Top = idx = bcnt = 0; memset (dfn,-1, sizeof (dfn )); memset (instack, 0, sizeof (instack); For (INT I = 0; I <2 * n; ++ I) if (dfn [I] =-1) tarjan (G, I);} PRIVATE: int top, idx, bcnt; int dfn [VN]; int low [VN]; int sta [VN]; int belong [VN]; bool instack [VN];} sat; bool OK () {for (INT I = 0; I <n; ++ I) {If (MAT [I] [I]! = 0) return false; For (Int J = I + 1; j <n; ++ J) if (MAT [I] [J]! = Mat [J] [I]) return false;} return true;} bool judge () {// enumerated binary bits for (int K = 0; k <31; ++ K) {G. init (); For (INT I = 0; I <n; ++ I) {for (Int J = I + 1; j <n; ++ J) {int u = I, V = J, W = (MAT [I] [J]> K) & 1; if (I % 2 = 0 & J % 2 = 0) {If (w) {G. addedge (u * 2, V * 2 + 1), G. addedge (V * 2, u * 2 + 1); // 0, 0g. addedge (u * 2, V * 2), G. addedge (V * 2 + 1, u * 2 + 1); // 0, 1g. addedge (u * 2 + 1, V * 2 + 1), G. addedge (V * 2, u * 2); // 1, 0} else {G. addedge (u * 2 + 1, V * 2), G. addedge (V * 2 + 1, u * 2); // 1, 1} else if (I % 2 = 1 & J % 2 = 1) {If (w) {G. addedge (u * 2, V * 2 + 1), G. addedge (V * 2, u * 2 + 1); // 0, 0} else {G. addedge (u * 2, V * 2), G. addedge (V * 2 + 1, u * 2 + 1); // 0, 1g. addedge (u * 2 + 1, V * 2 + 1), G. addedge (V * 2, u * 2); // 1, 0g. addedge (u * 2 + 1, V * 2), G. addedge (V * 2 + 1, u * 2); // 1, 1} else {// XOR if (w) {G. addedge (u * 2, V * 2 + 1), G. addedge (V * 2, u * 2 + 1); // 0, 0g. addedge (u * 2 + 1, V * 2), G. added Ge (V * 2 + 1, u * 2); // 1, 1} else {G. addedge (u * 2, V * 2), G. addedge (V * 2 + 1, u * 2 + 1); // 0, 1g. addedge (u * 2 + 1, V * 2 + 1), G. addedge (V * 2, u * 2); // 1, 0 }}} if (! Sat. Check (G, n) return false;} return true;} int main () {While (~ Scanf ("% d", & N) {G. init (); bool flag = true; For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) scanf ("% d", & mat [I] [J]); If (! OK () {puts ("no"); continue;} If (Judge () puts ("yes"); else puts ("no ");} return 0 ;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.