图论(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 ...
随机推荐
- IDL计算儒略日
遥感数据还有一些文章中使用数据的时候,经常使用儒略日(Julian day),即计算该天是一年中的第几天.正好有时间,就用IDL写了段儿小代码,方便使用. ;+ ; :Author: caoz ...
- 《REWORK》启示录 招聘笔杆子——程序员为什么值得写博客
Hire Great Writers 仿佛这是写给自己看的,不过这在其中也有着相当有趣的意义 .虽然自己算是一个能写的人,或许这算是一种不算才华的才华,写博文的意义通常不会在于去描述自己怎样,怎样.通 ...
- SQL常用的语句和函数
order by 的数值型灵活使用 select * from table_a where order by decode(函数,'asc',1,'desc',-1)*jsny; 控制试图的访问时间: ...
- 美好头标ToolBar
ActionBar我相信是每一位合格的程序员都用过的组件,也是每一个程序员都会抱怨的组件,因为他不能实现复杂的自定义.为此Google推出了比ActionBar更为美好的组件ToolBar. 本文重点 ...
- EntityFrameowk6.1 使用enum和低版本的不同
原有项目中使用EF5.0 实体类 public partial class Log : BaseEntity { public Nullable<int> LogLevelId { get ...
- There is no ID/IDREF binding for IDREF
http://blog.csdn.net/greensurfer/article/details/7596219
- 十三、C# 事件
1.多播委托 2.事件 3.自定义事件 在上一章中,所有委托都只支持单一回调. 然而,一个委托变量可以引用一系列委托,在这一系列委托中,每个委托都顺序指向一个后续的委托, 从而形成了一个委托链,或 ...
- MATLAB中mexFunction函数的接口规范
MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...
- POJ 2112.Optimal Milking (最大流)
时间限制:2s 空间限制:30M 题意: 有K台挤奶机(编号1~K),C头奶牛(编号K+1~K+C),给出各点之间距离.现在要让C头奶牛到挤奶机去挤奶,每台挤奶机只能处理M头奶牛,求使所走路程最远的奶 ...
- 【POJ2406】【KMP】Power Strings
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...