题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4879

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ; struct Two_Sat
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int s[maxn*],cnt; void init(int n)
{
this->n = n;
memset(mark,,sizeof(mark));
for(int i=; i<=n*; i++) G[i].clear();
} void add_clause(int u,int v,int flag,char symbol)
{
u = u* + flag;
v = v* + flag;
if(symbol == 'A')
{
if(flag)
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(u);
G[v^].push_back(v);
}
else
{
G[u^].push_back(v);
G[v^].push_back(u);
}
}
else if(symbol == 'O')
{
if(flag)
{
G[u^].push_back(v);
G[v^].push_back(u);
}
else
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(u);
G[v^].push_back(v);
}
}
else
{
if(flag)
{
G[u^].push_back(v);
G[v].push_back(u^);
G[v^].push_back(u);
G[u].push_back(v^);
}
else
{
G[u].push_back(v);
G[v].push_back(u);
G[u^].push_back(v^);
G[v^].push_back(u^);
}
}
} bool dfs(int u)
{
if(mark[u^]) return false;
if(mark[u]) return true;
mark[u] = true;
s[cnt++] = u;
for(int i=; i<G[u].size(); i++)
{
if(!dfs(G[u][i])) return false;
}
return true;
} bool solve()
{
for(int i=; i<n*; i+=)
{
if(!mark[i] && !mark[i+])
{
cnt = ;
if(!dfs(i))
{
while(cnt>) mark[s[--cnt]] = false;
if(!dfs(i+)) return false;
}
}
}
return true;
}
} solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int N; while(cin>>N)
{
int b[][];
bool ans = true;
for(int i=; i<N; i++)
for(int j=; j<N; j++)
scanf("%d",&b[i][j]); for(int i=; i<N; i++)
for(int j=i; j<N; j++)
{
if(i == j && b[i][j] != )
{
ans = false;
break;
}
if(b[i][j] != b[j][i])
{
ans = false;
break;
}
}
if(!ans)
{
printf("NO\n");
continue;
}
for(int k=; k<; k++)
{
solver.init(N);
for(int i=; i<N; i++)
for(int j=i+; j<N; j++)
{
char symbol;
if(i% == && j% == ) symbol = 'O';
else if(i% == && j% == ) symbol = 'A';
else symbol = 'X'; if(b[i][j] & (<<k))
solver.add_clause(i,j,,symbol);
else
solver.add_clause(i,j,,symbol); }
if(!solver.solve())
{
ans = false;
break;
}
}
if(ans) printf("YES\n");
else printf("NO\n");
}
}

zoj 3656的更多相关文章

  1. zoj 3656 2-sat 不错的题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4879 TLE了一下午.然后没办法了 去搜题解 发现思路跟我的差点儿相同 可是 ...

  2. HDU 4421 ZOJ 3656 Bit Magic

    2-SAT,不要所有位置全部建好边再判断,那样会MLE的. 正解是,每一位建好边,就进行一次2-SAT. #include<cstdio> #include<cstring> ...

  3. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  4. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  5. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  6. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  7. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  8. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  9. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

随机推荐

  1. ria service 单元测试

    https://blogs.msdn.microsoft.com/kylemc/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-1-the-i ...

  2. jquery 中$('.ui-selecter',this)这样写 是什么意思 求解释 见到都是这种$("div")

    如果用$("div")是拿选择所有的div$('.ui-selecter',this)选择的是现在选项中的child里面class是ui-selecter的意思跟jQuery(th ...

  3. oc 获取当前时间

    //获取当前日期 NSDate* date = [NSDate date]; NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; ...

  4. C++ 实现01背包动态规划

    简述一下01背包: 背包容量大小固定,有一些物品,每个物品都有重量和价值两个属性,且物品唯一不重复(即同一物品只能放入一个),放入物品的总重量不能超过背包容量 ,求放入背包的物品的总价值最大化.0代表 ...

  5. 237. Delete Node in a Linked List(C++)

    237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly l ...

  6. hdu 1788 Chinese remainder theorem again(最小公倍数)

    Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...

  7. NLPIR.user Not valid license or your license expired! Please feel free to contact pipy_zhang@msn.com

    NLPIR.user Not valid license or your license expired! Please feel free to contact pipy_zhang@msn.com ...

  8. 移动端Web开发如何处理横竖屏

    <!Doctype html> <html> <head> <meta charset="utf-8"> <meta id=& ...

  9. Xcode7之后常见问题整理-b

    一.Xcode7,iOS9之后传出来的什么Xcode有鬼,被植入代码片段什么的,可以看看,了解一下http://drops.wooyun.org/news/8864 二.bitcode问题--未正确设 ...

  10. Distinct Substrings

    spoj694:http://www.spoj.com/problems/DISUBSTR/ 题意:给以一个串,求这个串的所有不同子串的个数. 题解:第一次接触后缀数组,这里可以转化成,求所有子串后缀 ...