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. AC自动机 - 多模式串的匹配运用 --- HDU 2896

    病毒侵袭 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=2896 Mean: 略 analyse: AC自动机的运用,多模式串匹配. ...

  2. Linq专题之创建Linq查询表达式

    本节我们主要介绍一下如何创建查询集合类型,关系数据库类型,DataSet对象类型和XML类型的数据源的Linq查询表达式. 下面在实例代码ReadyCollectionData()函数创建了准备的数据 ...

  3. 【循序渐进学Python】4. Python中的序列——字典

    字典是Python内建的六种序列之一.字典作为一种常用的数据结构,字典中的值没有特定顺序,每个值都对应于一个唯一的键.键可以是数字.字符串甚至是元组. 1. 创建和使用字典 Python中字典可以使用 ...

  4. csharp: MongoDB

    安装配置: Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-o ...

  5. ActiveReports 9 新功能:借助目录(TOC)控件为报表添加目录功能

    在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍新增文档目录控件(TOC),通过拖拽操作便可添加报表目录. ...

  6. 从几篇文字得到关于web app开发的性能问题的答案

    1. http://blogs.adobe.com/creativecloud/are-mobile-web-apps-slow/ 2. http://software.intel.com/zh-cn ...

  7. u-boot移植总结(三)(转)S3C2440对Nand Flash操作和电路原理(基于K9F2G08U0A)

    S3C2440对Nand Flash操作和电路原理(基于K9F2G08U0A) 转载自:http://www.cnblogs.com/idle_man/archive/2010/12/23/19153 ...

  8. php中的引用类型和值类型

    PHP中的四种简单类型和复杂类型array都是值类型.同类型间赋值传递的是值,即创建一个副本给新变量. 例如: $int1 = 123; $int2 = $int1;//直接传递的是值,只是做了一个叫 ...

  9. phantomjs 另类用法

    这几天接到了一个数据采集的项目 通过对网站的前期分析得到以下内容 1.网站本身采用.NET开发 2.需抓取内容采用DES加密 虽然得到了前端javascript的加解密算法,但大家也知道跨语言算法想要 ...

  10. javascript实现排序算法

    准备好好学习js了,js写的第一个排序 先推荐一个js在线编辑工具,RunJS,还不错. 冒泡排序 var arr = [2,4,1,5,3]; function handle(arr){ for(v ...