POJ 3678 2-SAT

题目链接

Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:

Xa op Xb = c
The calculating rules are:
AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0

Given a Katu Puzzle, your task is to determine whether it is solvable.

Input

The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
The following M lines contain three integers a (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, describing the edges.

Output

Output a line containing "YES" or "NO".

Sample Input

4 4

0 1 1 AND

1 2 1 OR

3 2 0 AND

3 0 0 XOR

Sample Output

YES

Hint
X0 = 1, X1 = 1, X2 = 0, X3 = 1.

题意

n个点,m个关系,每个关系给a,b,c三个数字,和一个字符串(or,and,xor)表示
a opt b == c,然后问你是否有一种取值满足所有关系

思路

2-SAT,xi 表示变量i取0, xi+n表示i取1.把关系变成有向图,如果i, i + n在同一强连通分量中那么显然关系错误。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>using namespace std;
const int MAXN = 2e3 + 7;
const int MAXM = 2e6 + 7;struct Node {int to, w, next;
} edge[MAXM * 2];int first[MAXN], sign, dfn[MAXN], low[MAXN], ins[MAXN], n, m, idx, tot, color[MAXN];stack<int>st;inline void init() {memset(first, -1, sizeof(first));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(ins, 0, sizeof(ins));memset(color, 0, sizeof(color));sign = idx = tot = 0;while(!st.empty()) {st.pop();}
}inline void add_edge(int u, int v, int w) {edge[sign].to = v;edge[sign].w = w;edge[sign].next = first[u];first[u] = sign++;
}void tarjan(int x) {low[x] = dfn[x] = ++idx;st.push(x), ins[x] = 1;for(int i = first[x]; ~i; i = edge[i].next) {int to = edge[i].to;if(!dfn[to]) {tarjan(to);low[x] = min(low[x], low[to]);} else if(ins[to]) {low[x] = min(low[x], dfn[to]);}}int top;if(dfn[x] == low[x]) {tot++;do {top = st.top();st.pop();ins[top] = 0;color[top] = tot;} while(top != x);}
}void TwoSAT() {for(int i = 1; i <= n; i++ ) {if(!dfn[i]) {tarjan(i);}}bool ok = 1;for(int i = 1; i <= n; i++ ) {if(color[i] == color[i + n]) {ok = 0;break;}}puts(ok ? "YES" : "NO");
}int main() {int a, b, c;char opt[15];while(~scanf("%d %d", &n, &m)) {init();for(int i = 1; i <= m; i++ ) {scanf("%d %d %d %s", &a, &b, &c, opt);a++, b++;if(opt[0] == 'A') {if(c == 1) { /// A and B == 1;add_edge(a, a + n, 1), add_edge(b, b + n, 1);add_edge(a + n, b + n, 1), add_edge(b + n, a + n, 1);} else {     /// A and B == 0;add_edge(a + n, b, 1);add_edge(b + n, a, 1);}} else if(opt[0] == 'O') {if(c == 1) { /// A or B == 1;add_edge(a, b + n, 1), add_edge(b, a + n, 1);} else {     /// A or B == 0;add_edge(a, b, 1), add_edge(b, a, 1);add_edge(a + n, a, 1), add_edge(b + n, b, 1);}} else if(opt[0] == 'X') {if(c == 1) { /// A xor B == 1;add_edge(a, b + n, 1), add_edge(b, a + n, 1);add_edge(a + n, b, 1), add_edge(b + n, a, 1);} else {     /// A xor B == 0;add_edge(a, b, 1), add_edge(b, a, 1);add_edge(a + n, b + n, 1), add_edge(b + n, a + n, 1);}}}TwoSAT();}return 0;
}

转载于:https://www.cnblogs.com/Q1143316492/p/9179541.html

POJ 3678 2-SAT简单题相关推荐

  1. POJ 3458 Colour Sequence(简单题)

    [题意简述]:事实上题意我也没有特别看懂.可是依据它少许的题目描写叙述加上给的例子.就大胆的做了例如以下的推測: 就是说,如今给出一串字符s.然后紧接着给出可见的字符串visible还有隐藏的字符串h ...

  2. HDU 排名(简单题)

    好久没在oj上做题了,刚开始第二天做一道简单题的心得记录. 1 #include <cstdio> 2 #include <cstring> 3 #include <st ...

  3. (每日一题)CF1139D Steps to One 2021年天梯赛 L3-3 可怜的简单题(期望,莫比乌斯反演,杜教筛)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 一周连考三门,人都没了 Weblink 2021年天梯赛 L3-3 可怜的简单题 CF1139D Pr ...

  4. 【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题

    Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下: 有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取 ...

  5. 小M的简单题(easy)

    小M的简单题(easy) 时间限制:3s 内存限制:128MB [题目描述] 小M是某知名高中的学生,有一天,他请他的n个同学吃苹果,同学们排成一行,且手中已经有一些苹果.为了表示他的大方,有时他会给 ...

  6. Loj 【CQOI 2006】简单题,mmp

    #10117. 「一本通 4.1 练习 2」简单题   题目描述 题目来源:CQOI 2006 有一个 nnn 个元素的数组,每个元素初始均为 000.有 mmm 条指令,要么让其中一段连续序列数字反 ...

  7. BZOJ4066: 简单题

    BZOJ4066: 简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...

  8. BZOJ 4066: 简单题

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MB Submit: 2373  Solved: 622 [Submit][Status][Discuss ...

  9. CSU 1785: 又一道简单题

    1785: 又一道简单题 Submit Page    Summary    Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 602 ...

  10. zzuli 2177 Contest - 河南省多校连萌(四)(简单题)

    Contest - 河南省多校连萌(四) Problem F: 小姐姐的忠告:少吃辣条多刷题 题目链接 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1 ...

最新文章

  1. 跟着“路线图”,我们一起遨游机器学习的世界!
  2. python基础30个常用代码大全-Python基础小题汇总
  3. 使用阿里云ACM简化你的Spring Cloud微服务环境配置管理
  4. Lesson 2.张量的索引、分片、合并以及维度调整
  5. Matlab的不同进制转换
  6. boost::make_ready_future相关的测试程序
  7. flutter GridView 九宫格
  8. tcp对连接断开的感知——保活定时器
  9. Java 用HTTP的方式发送JSON报文请求
  10. 【图像分割】基于matlab GAC水平集方法图像分割【含Matlab源码 389期】
  11. 用ajax来上传图片,使用AJAX上传图片
  12. 计算机中h是几进制,16进制后面用H表示,其他进制的用什么表示
  13. 【东南大学主办: IEEE ICSIP 2022】信号与图像处理热门主题,7月邀您相约苏州
  14. 高项_第十二章项目采购管理
  15. 分享50个漂亮的双屏桌面壁纸资源(下篇)
  16. 1-1HTML笔记总结
  17. 计算机专业刊头,浅谈报纸版面在电脑排版中的美学应用
  18. 使用MaxCompute搭建社交好友推荐系统
  19. MYSQL知识点记录-1
  20. 生活不像电影,生活比电影难多了

热门文章

  1. C语言日字,【C语言日日练(二)】static关键字
  2. 二十四、Java集合框架(一)
  3. Oracle 定时任务(dbms_jobs)、expdp 与 impdp 数据泵导入导出
  4. Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter、@WebListener
  5. VBS 控制 Windos 系统音量 及视频播放
  6. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_25-前后端请求响应流程小结...
  7. jQuery基础--选择器
  8. sql server book
  9. js和jQuery的总结
  10. 使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据