HDU 4421 ZOJ 3656 Bit Magic
2-SAT,不要所有位置全部建好边再判断,那样会MLE的。
正解是,每一位建好边,就进行一次2-SAT。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+; int N;
int B[][]; stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEdge(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} void dfs1(int now)
{
flag[now]=;
for(int i=; i<G[now].size(); i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=; i<FG[now].size(); i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=; i<*N; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=; i<N; i++)
if(Belong[i]==Belong[i+N])
return ;
return ;
} int main()
{
while(~scanf("%d",&N))
{
for(int i=; i<N; i++)
for(int j=; j<N; j++)
scanf("%d",&B[i][j]);
int Base=;
int ans=;
for(int i=; i<N; i++) if(B[i][i]!=) ans=;
for(int i=; i<N; i++)
for(int j=; j<N; j++)
if(B[i][j]!=B[j][i])
ans=;
if(ans==)
{
printf("NO\n");
continue;
} for(int k=; k<; k++)
{
init();
for(int i=; i<N; i++)
{
for(int j=i+; j<N; j++)
{
int c=B[i][j]&(<<k); if(i%==&&j%==)
{
if(c)
{
addEdge(i+N,j);
addEdge(j+N,i);
}
else
{
addEdge(i,i+N);
addEdge(j,j+N);
}
}
else if(i%==&&j%==)
{
if(c)
{
addEdge(i+N,i);
addEdge(j+N,j);
}
else
{
addEdge(i,j+N);
addEdge(j,i+N);
}
}
else
{
if(c)
{
addEdge(i,j+N);
addEdge(j,i+N);
addEdge(i+N,j);
addEdge(j+N,i);
}
else
{
addEdge(i,j);
addEdge(j,i);
addEdge(i+N,j+N);
addEdge(j+N,i+N);
}
}
}
}
if(!judge())
{
ans=;
break;
}
}
if(ans) printf("YES\n");
else printf("NO\n");
}
return ;
}
HDU 4421 ZOJ 3656 Bit Magic的更多相关文章
- HDU 4421 Bit Magic(2-sat)
HDU 4421 Bit Magic pid=4421" target="_blank" style="">题目链接 题意:就依据题目,给定b数 ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- HDU 4421 Bit Magic(奇葩式解法)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4421 题目大意: 给了你一段代码, 用一个数组的数 对其进行那段代码的处理,是可以得到一个矩阵 让你判 ...
- HDU 4421 Bit Magic (图论-2SAT)
Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or ( ...
- 图论(2-sat):HDU 4421 Bit Magic
Bit Magic Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 4421 Bit Magic
[题意] 这个函数是给A求B的,现在给你B,问你是否能有A的解存在. [2-SAT解法] 对于每个A[i]的每一位运行2-sat算法,只要跑到强连通就可以结束,应为只要判断是否有解,后面拓扑求解就不需 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4421(枚举+2-sat)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4421 思路:枚举32位bit,然后2-sat判断可行性,这里给出2-sat矛盾关系构图: 1.a&am ...
随机推荐
- find unique values in an array
Problem: given an array that contains duplicates (except one value), find the one value that does no ...
- js中的clientWidth offsetWidth scrollWidth等的含义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- c++ lower_bound upper_bound
lower_bound, first greater than or equal to upper_bound, first strickly greater
- MySQL数据库MyISAM和InnoDB存储引擎的比较【转载】
转自 http://www.cnblogs.com/panfeng412/archive/2011/08/16/2140364.html MySQL有多种存储引擎,MyISAM和InnoDB是其中常用 ...
- rm: 无法删除 "xxxxx.o" : 输入/输出错误.
rm: 无法删除 "xxxxx.o" : 输入/输出错误. 碰到无法删除的文件,以为完蛋了,要重装. 后面重启一下就可以了
- 【转】linux ls -l的详解
原文:http://blog.csdn.net/sjzs5590/article/details/8254527 以root的家目录为例: 可以看到,用ls -l命令查看某一个目录会得到一个7个字段的 ...
- 清除number输入框的上下箭头
<input type="number"/> 在chrome,firefox,safari浏览器上输入框右侧会有上下箭头 方法1: <input type=&qu ...
- spring @Autowired或@Resource 的区别
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属于spring的),默认情况下必 ...
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- java socket解析和发送二进制报文工具(附java和C++转化问题)
解析: 首先是读取字节: /** * 读取输入流中指定字节的长度 * <p/> * 输入流 * * @param length 指定长度 * @return 指定长度的字节数组 */ pu ...