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

版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

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

  1. HDU 4421 Bit Magic(2-sat)

    HDU 4421 Bit Magic pid=4421" target="_blank" style="">题目链接 题意:就依据题目,给定b数 ...

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

    Bit Magic Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 4421 Bit Magic(奇葩式解法)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4421 题目大意: 给了你一段代码, 用一个数组的数 对其进行那段代码的处理,是可以得到一个矩阵 让你判 ...

  4. hdu 4421 Bit Magic

    [题意] 这个函数是给A求B的,现在给你B,问你是否能有A的解存在. [2-SAT解法] 对于每个A[i]的每一位运行2-sat算法,只要跑到强连通就可以结束,应为只要判断是否有解,后面拓扑求解就不需 ...

  5. hdu 3183 A Magic Lamp(RMQ)

    题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...

  6. hdu 3183 A Magic Lamp RMQ ST 坐标最小值

    hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...

  7. HDU 3183.A Magic Lamp-区间找最小值-RMQ(ST)

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. hdu 4421(枚举+2-sat)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4421 思路:枚举32位bit,然后2-sat判断可行性,这里给出2-sat矛盾关系构图: 1.a&am ...

  9. hdu 4421 2-SAT问题

    思路:我们需要判断是否有满足的a[n],其实也就是对每一个二进制位进行判断,看是否有满足的.那么我们每次取出一个二进制位,这样每一位只有0,1两种状态,就成了比较典型的2-SAT问题了. #inclu ...

随机推荐

  1. BAT及各大互联网公司前端笔试面试题--Html,Css篇

    注意 转载须保留原文链接(http://www.cnblogs.com/wzhiq896/p/5931347.html )作者:wangwen896 整理分享出来希望更多的前端er共同进步吧,不仅适用 ...

  2. Winform开发框架之权限管理系统改进的经验总结(3)-系统登录黑白名单的实现

    在一般的权限系统里面,可能经常会看到系统的黑名单或者白名单的拦截功能.在一般权限系统里面,常见的黑名单就是禁止用户在某些IP上登录系统,白名单就是允许用户只在某些IP上登录系统.本随笔主要介绍在我的权 ...

  3. 一个让echarts中国地图包含省市轮廓的技巧

    背景知识及应用简介 本文主要介绍一个使用ECharts地图组件的取巧方法,该技巧源于实际需求中遇到的问题,一般没有该需求的话这个技巧也是用不到的.有前端基础和以及对ECharts有了解的人基本可以读懂 ...

  4. oracle的minus返回第一个表中有、第二个表中没有的数据

    oracle的minus返回第一个表中有.第二个表中没有的数据 CREATE TABLE hovertree_union_1 ( id INT, val ) ); CREATE TABLE hover ...

  5. 用UltraISO制作支持windows 7的U盘启动盘

    用UltraISO制作U盘启动盘,有人写过,我也看过,不过依照网上的那些文章,成功的并不多,经过几次试验,在不同的主板环境下成功概率高的方法应该如下:   1. UltraISO建议9.3以上 2. ...

  6. TreeBuilder科学的树创建器

    public static class TreeBuilder { public static List<dynamic> Build(IEnumerable<dynamic> ...

  7. 【C#进阶系列】08 方法

    实例构造与引用类型 之前的章节其实已经写过了引用类型的构造过程: 首先当然是,在堆中,为引用类型的实例对象分配内存,然后初始化对象的附加字段(即类型对象指针和同步块索引). 这个时候为对象分配的内存都 ...

  8. 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板

    [源码下载] 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 涂鸦板 通过 Poin ...

  9. 不变(Invariant), 协变(Covarinat), 逆变(Contravariant) : 一个程序猿进化的故事

    阿袁工作的第1天: 不变(Invariant), 协变(Covarinat), 逆变(Contravariant)的初次约 阿袁,早!开始工作吧. 阿袁在笔记上写下今天工作清单: 实现一个scala类 ...

  10. xmapp的安装

    搭建网站常识性的你首先得搭建一个服务器. 首先APACHE是世界使用排名第一的WEB服务器软件,但是安装APACHE WEB服务器并不容易.如果你想添加MYSQL.PHP和PERL,那就更难了.所以可 ...