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 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
  注意数组要开大。
  貌似并查集也可以做。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int maxm=;
int n,cnt,fir[maxn],to[maxm*],nxt[maxm*];
int tot,scnt,ID[maxn],low[maxn],scc[maxn]; int min(int a,int b){
return a<b?a:b;
} void addedge(int a,int b){
nxt[++cnt]=fir[a];
fir[a]=cnt;
to[cnt]=b;
} bool Inst[maxn];
int st[maxn],top;
void Tarjan(int x){
low[x]=ID[x]=++tot;
st[++top]=x;Inst[x]=true;
for(int i=fir[x];i;i=nxt[i])
if(!ID[to[i]]){
Tarjan(to[i]);
low[x]=min(low[x],low[to[i]]);
}
else if(Inst[to[i]])
low[x]=min(low[x],ID[to[i]]);
if(low[x]==ID[x]){
++scnt;
while(true){
int y=st[top--];
scc[y]=scnt;
Inst[y]=false;
if(x==y)break;
}
}
} bool Check(){
for(int i=;i<n*;i++)
if(!ID[i])Tarjan(i);
for(int i=;i<n;i++)
if(scc[i*]==scc[i*+])
return false;
return true;
} int b[][];
void Init(){
memset(fir,,sizeof(fir));
memset(scc,,sizeof(scc));
memset(ID,,sizeof(ID));
cnt=tot=scnt=;
} bool Judge(){
for(int t=;t<=;t++){
Init();
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i==j){if(b[i][j])return false;continue;}
else if((i&)&&(j&)){
if(b[i][j]&(<<t)){
addedge(i*,j*+);
addedge(j*,i*+);
}
else{
addedge(i*+,i*);//这两句并不影响答案,有谁知道为啥
addedge(j*+,j*);//
addedge(i*,j*);
addedge(j*,i*);
}
}
else if(!((i&)||(j&))){
if(b[i][j]&(<<t)){
addedge(i*,i*+);
addedge(j*,j*+);
addedge(i*+,j*+);
addedge(j*+,i*+);
}
else{
addedge(i*+,j*);
addedge(j*+,i*);
}
}
else{
if(b[i][j]&(<<t)){
addedge(i*+,j*);
addedge(j*+,i*);
addedge(i*,j*+);
addedge(j*,i*+);
}
else{
addedge(i*,j*);
addedge(j*,i*);
addedge(i*+,j*+);
addedge(j*+,i*+);
}
}
}
}
if(!Check())
return false;
}
return true;
} int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d",&b[i][j]);
if(Judge())
printf("YES\n");
else
printf("NO\n");
}
return ;
}

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

  1. HDU 4421 Bit Magic(2-sat)

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

  2. HDU 4421 Bit Magic (图论-2SAT)

    Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or ( ...

  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 ZOJ 3656 Bit Magic

    2-SAT,不要所有位置全部建好边再判断,那样会MLE的. 正解是,每一位建好边,就进行一次2-SAT. #include<cstdio> #include<cstring> ...

  9. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

随机推荐

  1. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  2. Android开发手记(25) 简单Service的实现

    本文将通过实现一个简单的Service发送简单消息,然后通过一个BroadcastReceiver接收Service发送的消息,从而改变一个TextView的文本颜色. 这里,我们需要三个java文件 ...

  3. vpn的作用

    1.可以用于远程对方桌面. 步骤: 1.浏览器中访问网址,输入用户名,密码即可 2.输入远程桌面用户名和网址

  4. 解决vim不能使用方向键和退格键问题

    1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母,或者退格键却变成方向键的功能 只要重装一下vi的依赖包即可完美解决vi编辑器方向键变字母的问题.rpm -e vim-enhance ...

  5. Vijos1675 NOI2005 聪聪和可可 记忆化搜索

    简单题,结果因为理解错题意懵逼了好久…… moveTo[x][y]表示聪聪在节点x,可可在节点y时,聪聪下一步应到达哪一个节点 dp[x][y]表示聪聪在节点x,可可在节点y,且轮到可可行动时,所需时 ...

  6. ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)

    [热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...

  7. 谈一下关于C++函数包装问题

    在C++中,我们经常遇到在某个特定的时刻,需要将函数进行包装调用,尤其是当我们需要将不同签名的函数放到同一个集合时,由于函数签名不一致导致我们不能直接将各式各样的函数指针放到诸如list这样的集合中, ...

  8. jQuery慢慢啃之事件(七)

    1.ready(fn)//当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. $(document).ready(function(){ // 在这里写你的代码...}); 使用 $(docume ...

  9. URPF 简单流程

    主要功能是防止基于源地址欺骗的网络攻击. 路由器接口一旦使能URPF功能,当该接口收到数据报文时,首先会对数据报文的源地址进行合法性检查,对于源地址合法性检查通过的报文,才会进一步查找去往目的地址的转 ...

  10. 初涉JavaScript模式 (3) : 字面量

    什么是字面量? 在编程语言中,字面量是一种表示值的记法.例如,"Hello, World!" 在许多语言中都表示一个字符串字面量(string literal ),JavaScri ...