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的更多相关文章

  1. HDU 4421 Bit Magic(2-sat)

    HDU 4421 Bit Magic pid=4421" target="_blank" style="">题目链接 题意:就依据题目,给定b数 ...

  2. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  3. HDU 4421 Bit Magic(奇葩式解法)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4421 题目大意: 给了你一段代码, 用一个数组的数 对其进行那段代码的处理,是可以得到一个矩阵 让你判 ...

  4. HDU 4421 Bit Magic (图论-2SAT)

    Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or ( ...

  5. 图论(2-sat):HDU 4421 Bit Magic

    Bit Magic Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. hdu 4421 Bit Magic

    [题意] 这个函数是给A求B的,现在给你B,问你是否能有A的解存在. [2-SAT解法] 对于每个A[i]的每一位运行2-sat算法,只要跑到强连通就可以结束,应为只要判断是否有解,后面拓扑求解就不需 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. 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 ...

  9. hdu 4421(枚举+2-sat)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4421 思路:枚举32位bit,然后2-sat判断可行性,这里给出2-sat矛盾关系构图: 1.a&am ...

随机推荐

  1. Chapter 2 Open Book——11

    "Hey, Dad, welcome home." hey爸爸,欢迎回家. "Thanks." He hung up his gun belt and step ...

  2. find the closest sum to a target value

    problem: given an array of integers including positive and negative, a target value. find 2 numbers ...

  3. Inno Setup入门(二十二)——Inno Setup类参考(8)

    : Install Setup 2013-02-02 11:31 477人阅读 评论(0) 收藏 举报 列表框 列表框(ListBox)是Windows应用程序中重要的输入手段,其中包括多个选项用户可 ...

  4. minicom 不能输入

    minicom -s  进入设置界面 在 Serial port setup 中设置 硬件控制流为NO F - Hardware Flow Control : No 就OK了

  5. SQL的自增列重置的方法

    SQL的自增列挺好用,只是开发过程中一旦删除数据,标识列就不连续了 写起来 也很郁闷,所以查阅了一下标识列重置的方法 发现可以分为三种: --- 删除原表数据,并重置自增列 truncate tabl ...

  6. 8.5 sikuli 集成进eclipse 报错:can't be found on the disk

    运行提示can't be found on the disk

  7. Hibernate 系列教程16-二级缓存

    pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate- ...

  8. C语言根据函数名调用对应的函数

    通过函数指针定义,调用时加上参数 struct Command { const char *name; const char *desc; // return -1 to force monitor ...

  9. c++模板两个数的加法

    1.最简单的情况: template<class T> T Add(const T& a, const T& b) { return a + b; } 缺点是不能够处理不同 ...

  10. mysql Group By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...