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. MySQL日志概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3859866.html ...

  2. js request

    比如你要获取aaa.aspx?id=2 使用方法为:var id= request('id'); 

  3. O2O的理解

    O2O 就是把线上和线下的优势完美结合,网上优惠价格,线下享受贴身服务!O2O 还可以实现不同商家的联盟 两年前的微博营销,现在没落了,那么微信营销呢,只能说来的太快了.线上来筛选服务和产品,所有的交 ...

  4. Openblas编译Android NDK库的步骤

    1.配置Android NDK编译工具.以下下载地址,直接放到浏览器中下载,不需要VPNlinux 32 bithttp://dl.google.com/android/ndk/android-ndk ...

  5. windows sever 2008 r2 - 限制ip访问

    和win 7 旗舰版不同,该操作系统在安装IIS后,非本机的并不能直接访问主机.需要设置主机上的本机的IIS中的IP地址和域限制. 由于我是想在同一个局域网(路由器)中,通过Android操作系统访问 ...

  6. 那些年,我们一起学WCF--(8)Single实例行为

    Single实例行为,类似于单件设计模式,所有可以客户端共享一个服务实例,这个服务实例是一个全局变量,该实例第一次被调用的时候初始化,到服务器关闭的时候停止. 设置服务为Single实例行为,只要设置 ...

  7. SQL觸發器聯級刪除

    Create TRIGGER [dbo].[trigInstructionsDelete] ON dbo.Instructions instead OF DELETE AS BEGIN DECLARE ...

  8. J2se中的声音---AudioPlayer

    1 package cn.gp.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import ...

  9. PHPMailer发匿名邮件及Extension missing: openssl的解决

    原文链接:http://www.tongfu.info/phpmailer%E5%8F%91%E5%8C%BF%E5%90%8D%E9%82 %AE%E4%BB%B6%E5%8F%8Aextensio ...

  10. CSS实现DIV三角形

    本文内容收集来自网络 #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px s ...