Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

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:

XaopXb = 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

  1. 4 4
  2. 0 1 1 AND
  3. 1 2 1 OR
  4. 3 2 0 AND
  5. 3 0 0 XOR

Sample Output

  1. YES

Hint

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

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<stack>
  5. #include<vector>
  6. #include<algorithm>
  7. using namespace std;
  8. #define MAX 1000000+10
  9. int low[MAX],dfn[MAX];
  10. int sccno[MAX],m,n;
  11. int scc_cnt,dfs_clock;
  12. bool Instack[MAX];
  13. vector<int>G[MAX];
  14. stack<int>s;
  15. void init()
  16. {
  17. for(int i=0;i<2*n;i++)
  18. G[i].clear();
  19. }
  20. void getmap()
  21. {
  22. while(m--)
  23. {
  24. int a,b,c;
  25. char op[5];
  26. memset(op,'\0',sizeof(op));
  27. scanf("%d%d%d%s",&a,&b,&c,op);
  28. if(op[0]=='A')
  29. {
  30. if(c==1)
  31. {
  32. G[a+n].push_back(a);
  33. G[b+n].push_back(b);
  34. }
  35. else
  36. {
  37. G[a].push_back(b+n);
  38. G[b].push_back(a+n);
  39. }
  40. }
  41. else if(op[0]=='O')
  42. {
  43. if(c==1)
  44. {
  45. G[a+n].push_back(b);
  46. G[b+n].push_back(a);
  47. }
  48. else
  49. {
  50. G[a].push_back(a+n);
  51. G[b].push_back(b+n);
  52. }
  53. }
  54. else if(op[0]=='X')
  55. {
  56. if(c==1)
  57. {
  58. G[a+n].push_back(b);
  59. G[b+n].push_back(a);
  60. G[a].push_back(b+n);
  61. G[b].push_back(a+n);
  62. }
  63. else
  64. {
  65. G[a].push_back(b);
  66. G[b].push_back(a);
  67. G[a+n].push_back(b+n);
  68. G[b+n].push_back(a+n);
  69. }
  70. }
  71. }
  72. }
  73. void tarjan(int u,int fa)
  74. {
  75. int v;
  76. low[u]=dfn[u]=++dfs_clock;
  77. Instack[u]=true;
  78. s.push(u);
  79. for(int i=0;i<G[u].size();i++)
  80. {
  81. v=G[u][i];
  82. if(!dfn[v])
  83. {
  84. tarjan(v,u);
  85. low[u]=min(low[v],low[u]);
  86. }
  87. else if(Instack[v])
  88. low[u]=min(low[u],dfn[v]);
  89. }
  90. if(low[u]==dfn[u])
  91. {
  92. ++scc_cnt;
  93. for(;;)
  94. {
  95. v=s.top();
  96. s.pop();
  97. Instack[v]=false;
  98. sccno[v]=scc_cnt;
  99. if(v==u) break;
  100. }
  101. }
  102. }
  103. void find(int l,int r)
  104. {
  105. memset(low,0,sizeof(low));
  106. memset(dfn,0,sizeof(dfn));
  107. memset(sccno,0,sizeof(sccno));
  108. memset(Instack,false,sizeof(Instack));
  109. scc_cnt=dfs_clock=0;
  110. for(int i=l;i<=r;i++)
  111. if(!dfn[i]) tarjan(i,-1);
  112. }
  113. void solve()
  114. {
  115. for(int i=0;i<n;i++)
  116. {
  117. if(sccno[i]==sccno[i+n])
  118. {
  119. printf("NO\n");
  120. return ;
  121. }
  122. }
  123. printf("YES\n");
  124. }
  125. int main()
  126. {
  127. while(scanf("%d%d",&n,&m)!=EOF)
  128. {
  129. init();
  130. getmap();
  131. find(0,2*n-1);
  132. solve();
  133. }
  134. return 0;
  135. }

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\) . 首先来看 ...

  10. POJ 3678 Katu Puzzle(强连通 法)

    题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...

随机推荐

  1. Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  2. 问题集锦 ~ CSS

    #button标签点击后出现点边框 input  {outline: none;} button::-moz-focus-inner {border:  none;}

  3. python 编码问题解决方案

    1.UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) ...

  4. js 关于时间

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  5. auto_ftp_sh

    #!/usr/bin/env python # -*- coding:utf-8 -*-   import paramiko import time   mydate = time.strftime( ...

  6. 应用七:Vue之适配test环境变量(2.0版)

    在我们使用vue-cli创建的项目中,默认只有开发development和生产production两种环境变量:但在实际的项目开发过程中往往都会有测试环境,下面就来说一下如何适配测试环境test的环境 ...

  7. Java中类的定义

    成员变量:对应事物的属性 成员方法:对应事物的行为 类定义的格式 定义类:就是定义类的成员,包括成员变量和成员方法 成员变量:和以前定义变量几乎是一样的.只不过位置发生了改变.在类中,方法外. 成员方 ...

  8. 如何添加删除子网卡eth0:1(linux案例)

    这种方法实现了单网卡多IP,我的系统是centos7的,如何添加删除子网卡IP详细请看下面操作例子 添加子网卡IP:ifconfig  ens3:1  192.168.0.100/24        ...

  9. IOS - 零碎

    ---恢复内容开始--- 1.模拟器目录: ProjectNameApk.documents.library(cache.preference.cookies).temp 2.Edit-Refacto ...

  10. 自动化测试之firebug、firepath、IDE的使用

    1firebug安装-firefox添加组件-firebug 如图 firepath依赖于firebug 展示路径用,安装和firebug一样