hdu 4421

来源:互联网 发布:stringbuffer的源码 编辑:程序博客网 时间:2024/04/27 23:43

Bit Magic

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3209    Accepted Submission(s): 907


Problem Description
Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my achievement would get teacher's attention.
The key function is the code showed below.

There is no doubt that my teacher raised lots of interests in my work and was surprised to my talented programming skills. After deeply thinking, he came up with another problem: if we have the matrix table b[N][N] at first, can you check whether corresponding number table a[N] exists?
 

Input
There are multiple test cases.
For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 231 - 1)
 

Output
For each test case, output "YES" if corresponding number table a[N] exists; otherwise output "NO".
 

Sample Input
20 44 030 1 241 0 8624 86 0
 

Sample Output
YESNO


枚举每一位,每次做一次2sat


#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <string>#include <functional>#include <cmath>#include <set>#include <queue>#include <algorithm>#include <vector>#include <map>#include <stack>using namespace std;#define esp  1e-8const double PI = acos(-1.0);const double e = 2.718281828459;const int inf = 2147483647;const long long mod = 1000000007;typedef long long ll;void fre(){freopen("alex.txt", "r", stdin);freopen("alex.txt", "w", stdout);}inline void in(int &x){register char ch;while (ch = getchar(), (ch < '0' || ch > '9'));x = ch - '0';while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - '0';}inline void out(int x){register char hc[30];register int len = 0;hc[len++] = x % 10 + '0';while (x /= 10) hc[len++] = x % 10 + '0';for (int i = len - 1; i >= 0; i--) putchar(hc[i]);}const int maxn = 1015;struct node{int from, to, next;}edge[4000006];int hand[maxn], belong[maxn], dfn[maxn], low[maxn], vis[maxn], st[maxn];int fp[maxn];//建立对应SCC编号的映射   int color[maxn];//染色int In[maxn];//新图SCC的入度vector<int>G[maxn];//缩点后新图int tot, num, index, cnt, n, m;void addedge(int a, int b){edge[tot].from = a;edge[tot].to = b;edge[tot].next = hand[a];hand[a] = tot++;}void init(){memset(hand, -1, sizeof(hand));memset(belong, -1, sizeof(belong));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(vis, 0, sizeof(vis));memset(st, 0, sizeof(st));tot = 0;num = 0;index = 0;cnt = 0;}void Tarjan(int u){dfn[u] = low[u] = index++;vis[u] = 1;st[num++] = u;for (int i = hand[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (!dfn[v]){Tarjan(v);low[u] = min(low[u], low[v]);}else if (vis[v]){low[u] = min(low[u], dfn[v]);}}if (low[u] == dfn[u]){cnt++;int v;do{v = st[--num];vis[v] = 0;belong[v] = cnt;} while (u != v);}}int b[515][515];int main(){int i, j;while (~scanf("%d", &n)){int res = 0;for (i = 0; i < n; ++i){for (j = 0; j < n; ++j){scanf("%d", &b[i][j]);//in(b[i][j]);}}for (int k = 0; k < 31; ++k){init();for (i = 0; i < n; ++i){for (j = i + 1; j < n; ++j){int p = (b[i][j] & (1 << k));if (i % 2 == 1 && j % 2 == 1){if (p){addedge(i, j + n); // ~a -> baddedge(j, i + n);// ~b -> a}else{addedge(i + n, i); // a -> ~aaddedge(j + n, j); // b -> ~b}}else if (i % 2 == 0 && j % 2 == 0){if (p){addedge(i, i + n); // ~a -> aaddedge(j, j + n); // ~b -> baddedge(i + n, j + n); // a -> baddedge(j + n, i + n); // b -> a}else{addedge(i + n, j); // a ->~b;addedge(j + n, i); // b -> ~a}}else{if (p){addedge(i, j + n);//~a -> b;addedge(j, i + n);//~b -> a;addedge(i + n, j); // a -> ~baddedge(j + n, i); //b -> ~a}else{addedge(i, j);  //~a -> ~b;addedge(j, i); // ~b -> ~a;addedge(i + n, j + n); //a -> baddedge(j + n, i + n); // b -> a}}}}for (int i = 0; i < n * 2; ++i){if (!dfn[i])Tarjan(i);}for (i = 0; i < n; ++i){if (belong[i] == belong[i + n]){res = 1;break;}}if (res)break;}if (res)printf("NO\n");elseprintf("YES\n");}}


0 0