HDU 4421 Bit Magic (图论-2SAT)

Bit Magic

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

2012 Asia ChangChun Regional Contest

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  5061 5060 5059 5058 5057

题目大意:

给你b[i][j],问你a[i]是否冲突?

解题思路:

对于a[i]的每一位根据 d[i][j] 进行2at

解题代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=510;

struct edge{
    int u,v,next;
    edge(int u0=0,int v0=0){
        u=u0,v=v0;
    }
}e[maxn*maxn*4];

int head[maxn*2],cnt,N;
int dfn[maxn*2],low[maxn*2],color[maxn*2],index,nc;
bool mark[maxn*2];
vector <int> vec;

void adde(int u,int v){
    e[cnt]=edge(u,v),e[cnt].next=head[u],head[u]=cnt++;
}

void init(){
    vec.clear();
    index=nc=cnt=0;
    for(int i=0;i<=2*N;i++){
        dfn[i]=0;
        color[i]=head[i]=-1;
        mark[i]=false;
    }
}

void tarjan(int s){
    dfn[s]=low[s]=++index;
    mark[s]=true;
    vec.push_back(s);
    for(int i=head[s];i!=-1;i=e[i].next){
        int d=e[i].v;
        if(!dfn[d]){
            tarjan(d);
            low[s]=min(low[s],low[d]);
        }else if(mark[d]){
            low[s]=min(low[s],dfn[d]);
        }
    }
    if(low[s]==dfn[s]){
        int d;
        nc++;
        do{
            d=vec.back();
            vec.pop_back();
            color[d]=nc;
            mark[d]=false;
        }while(s!=d);
    }
}

bool sat2(){
    for(int i=0;i<2*N;i++){
        if(!dfn[i]) tarjan(i);
    }
    for(int i=0;i<N;i++){
        if(color[i]==color[i+N]) return false;
    }
    return true;
}

int n,b[maxn][maxn];

void build(int x){
    N=n;//N =sum point
    init();
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if( i%2==1 && j%2==1 ){//|
                if( b[i][j]&(1<<x) ){
                    adde(i,j+n);
                    adde(j,i+n);
                }else{
                    adde(i,j);
                    adde(j,i);
                }
            }else if( i%2==0 && j%2==0 ){//&
                if( b[i][j]&(1<<x) ){
                    adde(i+n,j+n);
                    adde(j+n,i+n);
                }else{
                    adde(i+n,j);
                    adde(j+n,i);
                }
            }else{//^
                if( b[i][j]&(1<<x) ){
                    adde(i,j+n);
                    adde(i+n,j);
                    adde(j,i+n);
                    adde(j+n,i);
                }else{
                    adde(i,j);
                    adde(j,i);
                    adde(i+n,j+n);
                    adde(j+n,i+n);
                }
            }
        }
    }
}

void input(){
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++){
        scanf("%d",&b[i][j]);
    }
}

bool solve(){
    for(int i=0;i<n;i++){
        if(b[i][i]!=0) return false;
        for(int j=i+1;j<n;j++){
            if(b[i][j]!=b[j][i]) return false;
        }
    }
    for(int i=0;i<=30;i++){
        build(i);
        if(!sat2()) return false;
    }
    return true;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        input();
        if(solve()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
时间: 2024-10-18 10:33:53

HDU 4421 Bit Magic (图论-2SAT)的相关文章

HDU 4421 Bit Magic(2-sat)

HDU 4421 Bit Magic 题目链接 题意:就依据题目,给定b数组.看能不能构造出一个符合的a数组 思路:把每一个数字的每一个二进制位单独考虑.就变成一个2-sat题目了,依据题目中的式子建立2-sat的边.然后每一位跑2-sat.假设每位都符合.就是YES,假设有一位不符合就是NO 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #incl

图论(2-sat):HDU 4421 Bit Magic

Bit Magic Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3040    Accepted Submission(s): 871 Problem Description Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I

HDU 4421 Bit Magic(奇葩式解法)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4421 题目大意: 给了你一段代码, 用一个数组的数 对其进行那段代码的处理,是可以得到一个矩阵 让你判断这个矩阵能否由一个数组转化而来. 思路: 既然每组数据可以得到,那么他肯定能消去. 我们用一个数组P[i][j] 保存 a[i]^a[j]   的值 a[i]^a[j] 我们可用 P[i][j] = P[i][j-1]^a[j-1]^a[j] 这样我们就可以找出所有 P[i][j] = a[i]^

hdu 4421 Bit Magic

Bit Magic Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2057    Accepted Submission(s): 590 Problem Description Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I

HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(图论-2SAT,图论-tarjan)

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim

hdu 4421 BitMagic

这是一道区域赛的题目,解法有许多,这边是2-sat的做法 题目大意:自己看题 分析:对于A[i]的每一位做2-SAT,判断是否可行. 主要是建图: 对于a&b=0  有 a-> ┐b, b-> ┐a a&b=1            ┐a->a , ┐b->b a|b=0            a-> ┐a,b-> ┐b a|b=1     ┐a->b, ┐b->a a^b=0 a->b,b->a, ┐a-> ┐b, ┐b-

HDU 4948 (傻比图论)

Kingdom Problem Description Teacher Mai has a kingdom consisting of n cities. He has planned the transportation of the kingdom. Every pair of cities has exactly a one-way road. He wants develop this kingdom from one city to one city. Teacher Mai now

hdu1824-Let&#39;s go home:图论2-SAT

关键在于找出一定矛盾的条件,设一队的3个人为(a,b,c),a为队长,那么(a不留下,b不留下)矛盾,(a不留下,c不留下)矛盾; 对于每一对队员,(a留下,b留下)矛盾. 把模型建好,剩下的就是套模板了. 1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 using namespace std; 5 6 const int maxn = 3000+10; 7 8 struct TwoSAT 9 {

hdu 3183 A Magic Lamp(RMQ)

题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个,使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数,每次取的时候保证后面能取的个数足够,并且取的数最小,查询最小的操作用RMQ优化. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 10005; int N, M, d[m