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. SpringMVC核心——映射问题

    一.SpringMVC 使用 RequestMapping 来解决映射问题. 二.在学习 RequestMapping 之前,首先来看一张图. 这张图表示的是发送一次 http 请求时,所包含的请求 ...

  2. URL 字符编码

    URL 编码会将字符转换为可通过因特网传输的格式. URL - 统一资源定位器 Web 浏览器通过 URL 从 web 服务器请求页面. URL 是网页的地址,比如http://www.cnblogs ...

  3. sql语句创建新登录名和设置权限

    use DBName go --新增用户 exec sp_addlogin '用户名','密码','默认数据库名' --添加登录 exec sp_grantdbaccess N'test' --使其成 ...

  4. 【C#进阶系列】09 关于参数的故事

    可选参数和命名参数 不多说,上代码,自然懂 class Program { static void Main(string[] args) { var troy = new Troy(); troy. ...

  5. 51Node 1364--- 最大字典序排列(树状数组)

    51Node  1364--- 最大字典序排列(树状数组) 1364 最大字典序排列 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个1至N ...

  6. IBATIS动态SQL(转)

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...

  7. Servlet获得Http请求,GET/POST

    Servlet获得Http请求 Http请求信息格式 请求行(方法提交方式,URI,Http协议版本) GET方式提交:URI会包含查询字符串 POST方式提交:URI不会包含查询字符串 请求头 Ho ...

  8. Java与线程

    导语 我们知道,new一个thread,调用它的start的方法,就可以创建一个线程,并且启动该线程,然后执行该线程需要执行的业务逻辑, 那么run方法是怎么被执行的呢? Java线程和os线程 os ...

  9. LINUX重启MYSQL的命令

    LINUX重启MYSQL的命令 标签: mysqllinuxservice脚本web服务server 2010-06-25 10:21 62152人阅读 评论(0) 收藏 举报  分类: Linux( ...

  10. Linux里如何查找文件内容

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...