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. 利用DropDownList实现下拉

    在视图的Model<Vo>里面我们需要使用IEnumerable来将别的列表的数据全部的转化为下拉列表.下面是关于在项目中实际的写法. 一:实现下拉属性列表的写法   通过使用Select ...

  2. Asp.NET MVC JSON序列化问题

    最近在做项目的时候遇到一个JSON序列化问题. 环境:ASP.NET MVC 4.0 数据库:SQL 2008 在将获取的数据从后台以json的形式传给前台的easyui treegrid绑定的时候通 ...

  3. 利用mciSendString播放音频

    最近在写音频播放器,不过有点懒散,开发进度很慢,一天只做了一点点东西.其实就是让程序能播放音频.这个在我大二学winform程序开发时书上有说,那是书上教的是用media player的COM组件,而 ...

  4. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  5. 在一个XAML中点击按钮,界面跳转到另一个XAML界面方法

    private void ButtonGo_camerapage(object sender, RoutedEventArgs e) { this.Content = new cameraPage() ...

  6. C#为工作Sql而产生的字符串分割小工具(很实用,你值得拥有)

    写在前面 为什么要写这个工具? 工作需要,拼接字符串头晕眼花拼接的,特别是in 查询,后面的参数太多,想在数据执行一些这个sql语句老费劲了. 看正文 工作所有的(后台)攻城狮们都会接触到sql语句, ...

  7. Hibernate Validator

    http://docs.jboss.org/hibernate/validator/4.2/reference/zh-CN/html_single/#example-group-interfaces

  8. Unity中启动VS时出现"Visual Studio 2010 Shell 无效的许可证数据"的解决办法

    (感觉还是cnblog好一点,刚注册成功赶紧把baidu hi的一篇文章搬过来试试) 一直用着Visual Studio 2013给Unity写代码,安装了SQL Server 2014后,在Unit ...

  9. Java集合框架之Collection接口

    Java是一门面向对象的语言,那么我们写程序的时候最经常操作的便是对象了,为此,Java提供了一些专门用来处理对象的类库,这些类库的集合我们称之为集合框架.Java集合工具包位于Java.util包下 ...

  10. C# on Visual Studio Code

    installation Download .NET Core SDK installer and install it. https://www.microsoft.com/net/download ...