## ZOJ 3656 Bit Magic 第37届ACM/ICPC长春赛区现场赛B题 （2-SAT）

Bit Magic

Time Limit: 8 Seconds      Memory Limit: 32768 KB

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.

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];
}
}
}


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] ≤ 2 ^ 31 - 1)

#### Output

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

#### Sample Input

2
0 4
4 0
3
0 1 24
1 0 86
24 86 0


#### Sample Output

YES
NO

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;

const int MAXN=1100;//

bool visit[MAXN];
queue<int>q1,q2;
//vector建图方法很妙
vector<vector<int> >dag;//缩点后的逆向DAG图
int n,m,cnt;

int id[MAXN],order[MAXN],ind[MAXN];//强连通分量，访问顺序，入度

void dfs(int u)
{
visit[u]=true;
for(i=0;i<len;i++)
order[cnt++]=u;
}
void rdfs(int u)
{
visit[u]=true;
id[u]=cnt;
for(i=0;i<len;i++)
}
void korasaju()
{
int i;
memset(visit,false,sizeof(visit));
for(cnt=0,i=0;i<2*n;i++)
if(!visit[i])
dfs(i);
memset(id,0,sizeof(id));
memset(visit,false,sizeof(visit));
for(cnt=0,i=2*n-1;i>=0;i--)
if(!visit[order[i]])
{
cnt++;//这个一定要放前面来
rdfs(order[i]);
}
}
bool solvable()
{
for(int i=0;i<n;i++)
if(id[2*i]==id[2*i+1])
return false;
return true;
}

{
}

int b[600][600];
int bit[33];
int main()
{
int N;
bit[0]=1;
for(int i=1;i<31;i++)bit[i]=2*bit[i-1];
while(scanf("%d",&N)!=EOF)
{
n=N;
bool flag=true;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
scanf("%d",&b[i][j]);
if(i==j&&b[i][j]!=0)flag=false;
}
if(!flag)
{
printf("NO\n");
continue;
}
for(int i=0;i<N;i++)
{
if(!flag)break;
for(int j=i+1;j<N;j++)
if(b[i][j]!=b[j][i])
{
flag=false;
break;
}
}
if(!flag)
{
printf("NO\n");
continue;
}
for(int k=0;k<31;k++)
{
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++)
{
if(i%2==1&&j%2==1)
{
int t1=i;
int t2=j;
if(b[i][j]&bit[k])
{
}
else
{
}
}
else if(i%2==0&&j%2==0)
{
int t1=i;
int t2=j;
if(b[i][j]&bit[k])
{
}
else
{
}
}
else
{
int t1=i;
int t2=j;
if(b[i][j]&bit[k])
{
}
else
{
}

}
}
korasaju();
if(!solvable())
{
flag=false;
break;
}
}
if(flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}

posted on 2012-10-16 10:50  kuangbin  阅读(1665)  评论(7编辑  收藏  举报

JAVASCRIPT: