题目链接: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. TUXEDO管理命令总结

    tmboot  启动服务: 参数说明: -l  lmid 启动逻辑服务器名为lmcd服务器上的所有进程 -g grpname 启动GROUP名为grpname的所有进程 -i  srvid 启动SRV ...

  2. (转)iOS被开发者遗忘在角落的NSException-其实它很强大

    转载自 http://www.jianshu.com/p/05aad21e319e iOS被开发者遗忘在角落的NSException-其实它很强大 字数597 阅读968 评论4 喜欢28 NSExc ...

  3. zoom与transform:scale的区别

    一. zoom特性 1. zoom是IE的私有属性,但目前除Firefox不支持外,其他浏览器支持尚好. 2.定义: zoom即变焦,可改变元素尺寸,属于真实尺寸.zoom:百分值/数值/normal ...

  4. Windows Phone 之文件下载进度和速度显示

    用http协议来下载网络上的文件,通常我们需要获取文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在windows phone 7下可以使用WebClient类来实现这一功能,HttpWeb ...

  5. Asp.net服务器控件在IE10下的不兼容问题

    Asp.net服务器控件在IE10下的不兼容问题 时间:2013-05-16 09:07点击: 89 次 [大 中 小] 相信很多使用IE10的童鞋们已经发现了这个问题,以下是本人在IE10标准模式下 ...

  6. HTML5 自适应rem布局

    (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ' ...

  7. fedora安装sublime text教程

    下载 http://pan.baidu.com/s/1eRkEegM 解压 终端中切换到下载文件的目录下,执行以下命令: sudo tar -jxvf sublime_text_3_build_308 ...

  8. jquery 中 form的使用

    纠结了一下 form 表单的提交响应事件,因为在表单中,form标签会让浏览器自动提交,而我一直写的是 $(".btn").click(function()) 提交按钮的响应事件, ...

  9. Meditation Guide

    Meditation “Stop!!!” don’t we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the da ...

  10. Silverlight中在MVVM模式下对DatagridRow选择控件封装

    在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...