图论(2-sat):HDU 4421 Bit Magic
Bit Magic
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3040 Accepted Submission(s): 871
(&), 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?
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)
#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的更多相关文章
- HDU 4421 Bit Magic(2-sat)
HDU 4421 Bit Magic pid=4421" target="_blank" style="">题目链接 题意:就依据题目,给定b数 ...
- HDU 4421 Bit Magic (图论-2SAT)
Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or ( ...
- HDU 4421 Bit Magic(奇葩式解法)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4421 题目大意: 给了你一段代码, 用一个数组的数 对其进行那段代码的处理,是可以得到一个矩阵 让你判 ...
- hdu 4421 Bit Magic
[题意] 这个函数是给A求B的,现在给你B,问你是否能有A的解存在. [2-SAT解法] 对于每个A[i]的每一位运行2-sat算法,只要跑到强连通就可以结束,应为只要判断是否有解,后面拓扑求解就不需 ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- hdu 3183 A Magic Lamp RMQ ST 坐标最小值
hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...
- 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 ...
- HDU 4421 ZOJ 3656 Bit Magic
2-SAT,不要所有位置全部建好边再判断,那样会MLE的. 正解是,每一位建好边,就进行一次2-SAT. #include<cstdio> #include<cstring> ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
随机推荐
- Java多线程——其他工具类CyclicBarrier、CountDownLatch和Exchange
CyclicBarrier 适用于:创建一组任务,它们并行地执行任务,然后在进行下一个步骤之前等待,直至所有任务完成.它使得所有的并行任务都将在栅栏处列队,因此可以一致地向前移动. 表示大家彼此等待, ...
- mac下开发环境常用操作与命令
[1] 修改hosts文件 vim /private/etc/hosts
- eclipse下:selenium+python自动化之Chrome driver
1.下载chromedriver.exe文件: 2.下载的chromedriver.exe文件放置在chrome的安装目录下XXX\Chrome\Application\ ; 3.设置path环境变量 ...
- Moving a Subversion Repository to Another Server
Moving a subversion repository from one server to another, while still preserving all your version h ...
- node http.get请求
var http = require('http'); var querystring = require('querystring') var url = 'http://www.baidu.com ...
- hdu2962 Trucking (最短路+二分查找)
Problem Description A certain local trucking company would like to transport some goods on a cargo t ...
- 读书笔记之 - javascript 设计模式 - 单体模式
单体是一个用来划分命名空间,并将一批相关方法和属性组织在一起的对象,如果它可以被实例化,那么它只能被实例化一次. 单体模式,就是将代码组织为一个逻辑单元,这个逻辑单元中的代码可以通过单一的变量进行访问 ...
- 1.dubbo的安装 quickstart
按照官网给定的指导,执行下面的步骤即可 1.Import the dubbo source code to eclipse project 在eclipse中安装git插件 egit 直接可以从git ...
- underscorejs-size学习
2.24 size 2.24.1 语法: _.size(list) 2.24.2 说明: 返回列表的长度. 示例一:返回数组.对象.字符串的长度 //取数组的长度 var length length ...
- 关于浮动float属性和position:absolute属性的区别
最近返回头看了很多书籍,一直在纠结float属性和absolute绝对定位的区别和使用的情况,给大家分享一下自己的心得和体会吧. 1,float属性 float属性意义是让元素拜托独占一行的霸道总裁, ...