hdu 4421 && zoj 3656 Bit Magic 2-sat题找规律水过 也可以用并查集

题目:

Bit Magic

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


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] <= 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
 

Source

2-sat还没看 除此之外还有并查集做法 不过建图方式很奇特,貌似有2-sat 的影子

http://blog.csdn.net/lasolmi/article/details/38979207

还可以直接找规律水过....

http://vawait.com/hdu-4421/

Mr.D 2-sat:

http://paste.ubuntu.com/24972439/


2-sat还不会,先用找规律水过

根据b[i][i-1]=a[i]^a[i-1],有a[i]=b[i][i-1]^a[i-1] 因此如果知道了a[0]就可以递推出整个a[]数组 然后就可以由此得到b进行判断了 同时由于位运算只与当前位有关,因此可以枚举32位中的每一位,如果所有位都能找到0或者1其中一种情况使得这一位与给定的b的这一位一致  输出yes 否则输出no

b[i][i-1]=a[i]^a[i-1]

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=510;

int n,b[maxn][maxn];
int a[maxn],d[maxn][maxn];//存储某一位信息

bool judge(int num,int bit){
    a[0]=num;//not  a[bit]...................................................
    //取出b矩阵的第bit 位放到d矩阵
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            d[i][j]=(b[i][j]>>bit)&1;//sb 般的写成了b = d .....
        }
    }
    //根据这一位的信息还原出整个a矩阵
    for(int i=1;i<n;++i) a[i] = d[i][i-1] ? 1 - a[i-1] : a[i-1];//a[i]=a[i-1]^d[i][i-1];
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            if(i==j){ if(d[i][j]) return false;}
            else if((i&1)==1 && (j&1)==1){ if(d[i][j]!=(a[i]|a[j])) return false;}//位运算括号不加也行 
            else if((i&1)==0 && (j&1)==0){ if(d[i][j]!=(a[i]&a[j])) return false;}
            else if(d[i][j]!=(a[i]^a[j])) return false;
        }
    }
    return true;
}

int main(){
    while(scanf("%d",&n)==1){
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                scanf("%d",&b[i][j]);
            }
        }
        bool f=1;
        for(int i=0;i<32;++i){//枚举每一位
            if(!judge(0,i)&&!judge(1,i)){//如果这一位无论是0还是1都无法满足b矩阵的条件
                puts("NO");
                f=0;
                break;
            }
        }
        if(f) puts("YES");
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值