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. Bash关闭输出(关闭正确、错误输出)

    利用&>重定向,不输出任何内容: echo hello &> /dev/null 关闭正确输出: echo hello 1> /dev/null 关闭错误输出: ec ...

  2. linux工具问题,tail -f 失效

    最近发现一个很奇怪问题: tail -f 不能实时的输出日志

  3. CSS3条件判断——@supports/window.CSS.supports()(转)

    CSS3条件判断,听起来"不明觉厉",如果你对CSS稍为熟悉一点的话,你会发现CSS中的"@media"就是条件判断之一.是的,在CSS3的条件判断规范文档中包 ...

  4. iOS7初体验(2)——单元测试

    在Xcode 4.6及以前的版本,一直觉得单元测试这部分功能做得很鸡肋,用起来感觉很别扭.这一次Xcode 5.0默认就引入了单元测试,赶快来看看看相比以前的版本有什么提升吧!~_~ 1.     首 ...

  5. SpringMVC4+thymeleaf3的一个简单实例(篇四:form表单数据验证)

    关于表单数据验证有很多中方法,这里我仅介绍JSR303注解验证.JSR303仅仅是一个规范,这里我们要用到它的一个实现:hibernate-validator. 注意在spring的配置文件sprin ...

  6. WPF DataBinding之我见

    原创,转载请注明出处:WPF DataBinding之我见 一.DataBinding介绍   数据绑定是在应用程序 UI 与业务逻辑之间建立连接的过程. 如果绑定具有正确设置并且数据提供正确通知,则 ...

  7. 封装cookie组件

    var Cookie = { // 读取 get: function(name){ var cookieStr = "; "+document.cookie+"; &qu ...

  8. php计算时间差/两个时间日期相隔的天数,时,分,秒.

    function timediff( $begin_time, $end_time ) { if ( $begin_time < $end_time ) { $starttime = $begi ...

  9. github的访问变慢了

    以下个人观点:把操作系统的自主研究还有处理器自主研究列入重点,还有互联网上的种种动作,我发现里面似乎揭示了某些迹象,科研真的不应该以牺牲大部分人的河法全益为代价甚至目的.当某一天win不可能出现在出厂 ...

  10. 练习2 B题 - 求绝对值

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 求实数 ...