题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定。问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定)

思路:题意很清晰了,难点在建图。要考虑所有可能的冲突:

当op为and:  (1)c为0时,其中1个必为0。

        (2)c为1时,两者必为1。要加两条边,形如 a0->a1。

当op为or:   (1)c为0时,两者必为0。要加两条边,形如 a1->a0。

        (2)c为1时,其中1个必为1。

当op为xor:  (1)c为0时,两者必定相同。

        (2)c为1时,两者必定不同。

都是按照冲突来建图就行,有没有解留给DFS去判定。

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
//#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=*+;
int res[N][N];
vector<int> vect[N*];
map<string,int> mapp; void init()
{
string tmp="AND";
mapp[tmp]=;
tmp="OR";
mapp[tmp]=;
tmp="XOR";
mapp[tmp]=;
}
int s[N*], col[N*], c;
bool color(int x)
{
if(col[x^]) return false;
if(col[x]) return true;
col[x]=;
s[c++]=x;
for(int i=; i<vect[x].size(); i++)
if(!color(vect[x][i])) return false;
return true;
} int cal(int n)
{
memset(col,,sizeof(col));
memset(s,,sizeof(s));
for(int i=; i<n; i+=)
{
if(!col[i]&&!col[i+])
{
c=;
if(!color(i))
{
while(c) col[s[--c]]=;
if(!color(i+)) return false;
}
}
}
return true;
} int main()
{
freopen("input.txt", "r", stdin);
init();
string op;
int n, m, a, b, c;
while(~scanf("%d%d",&n,&m))
{
memset(res,0xf0,sizeof(res));
for(int i=n*; i>=; i--) vect[i].clear(); for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
cin>>op;
res[a][b]=c;
int t=mapp[op];
//设i*2为0,i*2+1为1
if(t==) //and
{
if(c==) //其中必有1个为0
{
vect[a*+].push_back(b*);
vect[b*+].push_back(a*); }
else //两者必为1
{
vect[a*].push_back(a*+); //指向自己
vect[b*].push_back(b*+);
}
}
else if(t==) //or
{
if(c==) //两者必为0
{
vect[a*+].push_back(a*);
vect[b*+].push_back(b*);
}
else //其中必有1个为1
{
vect[a*].push_back(b*+);
vect[b*].push_back(a*+);
}
}
else //XOR
{
if(c==) //两者必定相同
{
vect[a*].push_back(b*);
vect[b*].push_back(a*);
vect[a*+].push_back(b*+);
vect[b*+].push_back(a*+);
}
else //两者必定不同
{
vect[a*].push_back(b*+);
vect[a*+].push_back(b*);
vect[b*].push_back(a*+);
vect[b*+].push_back(a*);
}
}
}
if(!cal(n<<)) puts("NO");
else puts("YES");
}
return ;
}

AC代码

POJ 3678 Katu Puzzle (2-SAT,常规)的更多相关文章

  1. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...

  2. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  3. POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3741 Descr ...

  4. poj 3678 Katu Puzzle(2-sat)

    Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...

  5. POJ 3678 Katu Puzzle (经典2-Sat)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2401 Descr ...

  6. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  7. poj 3678 Katu Puzzle 2-SAT 建图入门

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  8. POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan

    http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...

  9. POJ 3678 Katu Puzzle

    Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...

随机推荐

  1. WPF处理Windows消息

    WPF中处理消息首先要获取窗口句柄,创建HwndSource对象 通过HwndSource对象添加消息处理回调函数. HwndSource类: 实现其自己的窗口过程. 创建窗口之后使用 AddHook ...

  2. hdu 3518 Boring counting 后缀数组LCP

    题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output ...

  3. 详解Javascript中的Array对象

    基础介绍 创建数组 和Object对象一样,创建Array也有2种方式:构造函数.字面量法. 构造函数创建 使用构造函数的方式可以通过new关键字来声明,如下所示: 12 var arr = new ...

  4. The method of type must override a superclass method

    导入android项目时,报The method of type must override asuperclass method 一堆错误, 解决方法: 将编译的jdk与使用的jdk版本一致即可.

  5. 让用户打开你app的位置功能

    +运动 http://www.ccidnet.com/2015/0819/10014152.shtml 让你的app不再是一个购物网站, 而是一种生活方式, 逛街,在实体店逛街积累里程,兑换积分  送 ...

  6. Solr + Hadoop = Big Data Love

    FROM:http://architects.dzone.com/articles/solr-hadoop-big-data-love 许多人使用Hadoop的开源项目来处理大数据的大数据集,因为它是 ...

  7. sql之表连接 筛选条件放在 连接外和放在连接里的区别

    使用一个简单的例子,说明他们之间的区别 使用的表:[Sales.Orders]订单表和[Sales.Customers]客户表,和上一篇博客的表相同 业务要求:查询出 : 所有的用户 在 2012-1 ...

  8. linux下nginx的安装

    一.安装nginx     1.在nginx官方网站下载一个包,下载地址是:http://nginx.org/en/download.html     2.WinSCP(ftp上传工具).exe FT ...

  9. 在 Windows 8 或 8.1 上安装 .NET Framework 3.5 安装错误:0x800f0906、0x800F081F

    昨天给一天新装Windows 8.1的PC装.NET Framework 3.5 发现联网速度很慢,并且在长久等待过后直接报错了:0x800f0906 经过Bing,发现了解决方案: 如果根据需要安装 ...

  10. 学习javascript总结下来的性能优化的小知识(一)

    http://www.cnblogs.com/ctriphire/p/4115525.html http://www.cnblogs.com/xjchenhao/archive/2012/10/22/ ...