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 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 = cThe calculating rules are:

?

AND01
000
101
OR01
001
111
XOR01
001
110
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.

?

Source

POJ Founder Monthly Contest – 2008.07.27, Dagger
胡亂地搞,竟然就AC了
借用別人的解題報告

思路:因為給出結點 a ,b,值 c,還有判斷方式OP,這種一看當然就知道是用2SAT做了。為什么說是深刻理解2SAT呢,因為……2SAT中說過,只有關系確定的才能連邊,否則不能連邊;還有一個重要的是,如果某個條件必須為某個值時,自身與自身的相反條件也要連邊,具體看下面解釋:

現在設 2*a為1,2*a+1為0;當然 2*b為1,2*b+1為0:

1.當OP為’And‘時:

(1)當c=1時,那么只有a 與 b同時為1時,a ?AND b才等于1,并且有且只有當a與b都為1時這個條件才成立,所以a與b一定要等1,所以連邊<2*a+1,2*a>,<2*b+1,2*b>,表示不管怎么樣,a與b的情況都等于1,即:當a等于0時a必等于1,b等于0時b必等于1,這個剛開始我看別人的解題報告就是這么說的,然后自己也沒太理解,其實真正的內涵就是強制執行a與b都等于1 !(如果a等于1了的話當然這條邊就沒用了,如果a等于0的話,那么這條連就可以起到把a強制等于1以符合題目條件情況了,就是如此簡單,得慢慢理解)

(2)當c=0時,那么當a等于0時,b可能為0也可以為1,所以是不確定關系,由上面說的一定是確定關系才能連邊,所以a為0的情況就不能連邊了;當a等于1時,b一定為0才能使 a AND b =0,所以連邊:<2*a,2*b+1>,當然還有<2*b,2*a,+1>。

2.當OP為OR時,

(1)當c=1時,那么當a=1時,b=1或者b=0,所以當a=1時出現了兩種關系,就是不確定了,就不用連邊了;當a=0時,那么b一定=1,所以是確定關系,連邊:<2*a+1,2*b>,當然還有<2*b+1,2*a>。

(2)當c=0時,那么只有當a=b=0這個關系,所以這個和上面1(1)情況就一樣了,上面是強制執行a=b=1的情況,而這里因為只有a=b=0的情況,所以也要強制執行a=b=0,即連邊:<2*a,2*a+1>,<2*b,2*b+1>。

3.當OP為XOR時,因為如果a=1,那么b必=0;a=0,b必=1;b=1,a必=0;b=0,a必=1。如此看,這四個關系都是確定的,所以都要連邊,但是其實我們可以不連,一條邊都不用連,因為出a=1的時候一定不會再出現a=0了,這四條邊是不會產生矛盾的,所以強連通縮點后不會出現belong[2*a]=belong[2*a+1]的情況的,所以連了也沒用,只是多加了點判斷的時間罷了……這在別人的解題報告里說的是形成了組環了,都是一個意思。比如:a=1,b=0與b=0,a=1在tarjan中會形成一個新的結點,也就是自環,所以……在異或這種情況中只能選擇a=0或者a=1,所以不會出現矛盾……故不用連邊了!

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)  
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 1006
 23 #define inf 1e12
 24 int n,m;
 25 vector<int> e[N];
 26 
 27 int tot;
 28 int head[N];
 29 int vis[N];
 30 int tt;
 31 int scc;
 32 stack<int>s;
 33 int dfn[N],low[N];
 34 int col[N];
 35 struct Node
 36 {
 37     int from;
 38     int to;
 39     int next;
 40 }edge[N*N];
 41 void init()
 42 {
 43     tot=0;
 44     scc=0;
 45     tt=0;
 46     memset(head,-1,sizeof(head));
 47     memset(dfn,-1,sizeof(dfn));
 48     memset(low,0,sizeof(low));
 49     memset(vis,0,sizeof(vis));
 50     memset(col,0,sizeof(col));
 51 }
 52 void add(int s,int u)//鄰接矩陣函數
 53 {
 54     edge[tot].from=s;
 55     edge[tot].to=u;
 56     edge[tot].next=head[s];
 57     head[s]=tot++;
 58 }
 59 void tarjan(int u)//tarjan算法找出圖中的所有強連通分支
 60 {
 61     dfn[u] = low[u]= ++tt;
 62     vis[u]=1;
 63     s.push(u);
 64     int cnt=0;
 65     for(int i=head[u];i!=-1;i=edge[i].next)
 66     {
 67         int v=edge[i].to;
 68         if(dfn[v]==-1)
 69         {
 70         //    sum++;
 71             tarjan(v);
 72             low[u]=min(low[u],low[v]);
 73         }
 74         else if(vis[v]==1)
 75           low[u]=min(low[u],dfn[v]);
 76     }
 77     if(dfn[u]==low[u])
 78     {
 79         int x;
 80         scc++;
 81         do{
 82             x=s.top();
 83             s.pop();
 84             col[x]=scc;
 85             vis[x]=0;
 86         }while(x!=u);
 87     }
 88 }
 89 bool two_sat(){
 90     
 91     for(int i=0;i<2*n;i++){
 92         if(dfn[i]==-1){
 93             tarjan(i);
 94         }
 95     }
 96     for(int i=0;i<n;i++){
 97         if(col[2*i]==col[2*i+1]){
 98             return false;
 99         }
100     }
101     return true;
102 }
103 int main()
104 {
105     while(scanf("%d%d",&n,&m)==2){
106          init();
107          for(int i=0;i<N;i++) e[i].clear();
108          while(!s.empty()){
109              s.pop();
110          }
111         int a,b,c;
112         char s[6];
113         for(int i=0;i<m;i++){
114             scanf("%d%d%d%s",&a,&b,&c,s);
115             if(s[0]=='A'){
116                 if(c==1){
117                     //e[2*a+1].push_back(2*a);
118                     //e[2*b+1].push_back(2*b);
119                     add(2*a+1,2*a);
120                     add(2*b+1,2*b);
121                 }
122                 else{
123                     //e[2*a].push_back(2*b+1);
124                     //e[2*b].push_back(2*a+1);
125                     add(2*a,2*b+1);
126                     add(2*b,2*a+1);
127                 }
128             }
129             else if(s[0]=='O'){
130                 if(c==1){
131                     //e[2*a+1].push_back(2*b);
132                     //e[2*b+1].push_back(2*a);
133                     add(2*a+1,2*b);
134                     add(2*b+1,2*a);
135                 }
136                 else{
137                     //e[2*a].push_back(2*a+1);
138                     //e[2*b].push_back(2*b+1);
139                     add(2*a,2*a+1);
140                     add(2*b,2*b+1);
141                 }
142             }
143         }
144         if(two_sat()){
145             printf("YES\n");
146         }
147         else{
148             printf("NO\n");
149         }
150     }
151     return 0;
152 }
View Code

?

轉載于:https://www.cnblogs.com/UniqueColor/p/4814122.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375523.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375523.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375523.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

go 語言 first argument to append must be slice

錯誤代碼 func TestSliceGrowing(t *testing.T) {s : [4]int{1, 2, 3, 4}for i :0; i<10; i {s append(s, i)t.Log(len(s), cap(s))} }報錯代碼 s append(s, i)原因&#xff1a;append的第一個參數必須是切片 更正 func TestSliceGrowing(t *testing.T) {s : []int{1,…

豆瓣網靜態頁面

divcss網站登錄注冊豆瓣讀書視頻 音樂同城小組閱讀 豆瓣FM東西更多豆瓣視頻 影訊&購票電視劇排行榜 分類影評預告片 向后向前3/5正在熱映全部正在熱映>>即將上映 烈日灼心 4.7終結者&#xff1a;創世紀... 4.7百團大戰 4.7刺客&#xff1a;聶隱娘 4.7近期熱門更多影視…

C++并發編程實戰(豆瓣評分5.4)

評分已說明一切&#xff0c;切勿踩坑&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 推薦的翻譯 C并發編程實戰 關注公眾號回復【C并發編程實…

Please use boost/bind/bind.hpp + using namespace boost::placeholders

The practice of declaring the Bind placeholders (_1, _2, …) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. 提示w…

奔跑吧,兄弟

10月底的時候&#xff0c;不能忍受老婆的奚落&#xff0c;開始了我的跑步計劃。 說說&#xff0c;跑步需要注意的事項&#xff0c;首先你得有雙跑步鞋&#xff0c;我有一次是穿了薄底鞋跑的&#xff0c;結果&#xff0c;打滿了水泡。跑步前控制飲水&#xff0c;最好在飲食后2個…

2299 Ultra-QuickSort(歸并)

合并排序第一次。連環畫看著合并看著別人的博客的想法。http://poj.org/problem?id2299 #include <stdio.h> #include <stdlib.h>#define MAX 500001int n,a[MAX], t[MAX]; long long int sum;//歸并 void Merge(int l, int m, int r) {int p0;int il, jm1;while…

由openSession、getCurrentSession和HibernateDaoSupport淺談Spring對事物的支持

由openSession、getCurrentSession和HibernateDaoSupport淺談Spring對事物的支持 Spring和Hibernate的集成的一個要點就是對事務的支持&#xff0c;openSession、getCurrentSession都是編程式事務&#xff08;手動設置事務的提交、回滾&#xff09;中重要的對象&#xff0c;Hi…

【tool】沒有需求文檔的時候如何來設計測試用例

沒有需求文檔的時候如何來設計測試用例 1.根據客戶的功能點整理測試需求追朔表&#xff1a; 一般的客戶都要把要開發軟件的功能點寫成一個表格交給市場部&#xff0c;讓市場部門轉交研發部。所以客戶的功能點是編寫測試用例一個最最重要的依據。 2.根據開發人員的Software Spec…

go返回多個值和python返回多個值對比

go package mulVals_test import "testing" func returnMultiValues(n int)(int, int){return n1, n2 }func TestReturnMultiValues(t *testing.T) {// a : returnMultiValues(5)// 這里嘗試用一個值接受多個返回值&#xff0c;將編譯錯誤a, _ : returnMultiValues(…

努力學習 HTML5 (3)—— 改造傳統的 HTML 頁面

要了解和熟悉 HTML5 中的新的語義元素&#xff0c;最好的方式就是拿一經典的 HTML 文檔作例子&#xff0c;然后把 HTML5 的一些新鮮營養充實進入。如下就是我們要改造的頁面&#xff0c;該頁面很簡單&#xff0c;只包含一篇文章。 ApocalypsePage_Original.html&#xff0c;這是…

判斷系統是大端還是小段

大端&#xff1a;高位內存存儲低序字節小端&#xff1a;高位內存存儲高序字節short a 0x0102&#xff0c;其中 01 高序字節&#xff0c; 02 低序字節 #include<stdio.h>int main() {union {short s;char c[sizeof(short)];} un;un.s 0x0102;if (sizeof(short) 2) {if…

手機頁面head中的meta元素

<meta http-equiv"Pragma" content"no-cache"> <meta http-equiv"expires" content"0"> <meta http-equiv"cache-control" content"no-cache"> 清除瀏覽器中的緩存&#xff0c;它和其它幾句合起…

Delphi 關鍵 重啟 注銷

//在初始化的時候獲取權限 varhToken: THandle;Tkp: TTokenPrivileges;Zero: DWORD;beginOpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES orTOKEN_QUERY, hToken);LookupPrivilegeValue(nil, SeShutdownPrivilege, Tkp.Privileges[0].Luid);Tkp.PrivilegeCou…

C語言判斷系統是32位還是64位

long 在 32 位系統中是 4 字節&#xff0c;與 int 表示范圍相同&#xff0c;在 64 位系統中是 8 字節。 #include <stdio.h> #include <stdlib.h> #include <limits.h>int main() {long a INT_MAX;if (a 1 < 0) {printf("32: %ld\n", a);} e…

使用Eclipse搭建Struts2框架

本文轉自http://blog.csdn.net/liaisuo/article/details/9064527 今天在Eclipse搭建了Struts2 框架&#xff0c;并完成了一個很簡單的例子程序。 搭建好的全局圖如下: 第一步:在http://struts.apache.org/download.cgi下載Struts2的最新版即下載Full Distribution&#xff0c;這…

打印到類陣列的給定序列的所有排列的n皇后問題

題目例如以下&#xff1a;Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 分析&#xff1a;假設僅僅是求排列數非常好算&#xff0c;可是…

asp網絡編程:ASP如何獲取客戶端真實IP地址

要想透過代理服務器取得客戶端的真實IP地址&#xff0c;就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取。不過要注意的事&#xff0c;并不是每個代理服務器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取客戶端的真實…

autoLayout自動布局

autoLayout 有兩個核心概念&#xff1a; 約束&#xff1a;就是對控件進行高度&#xff0c;寬度&#xff0c;相對位置的控制 參照&#xff1a;多個控件時&#xff0c;一個或多個控件以其中的一個為基準進行高度&#xff0c;寬度&#xff0c;位置的設置 當選擇了 use auto layout…

C++11實現自旋鎖

參見 《深入理解C11》 #include <thread> #include <atoimic> #include <iostream> #include <unistd.h> using namespace std;std::atomic_flag lock ATOMIC_FLAG_INIT; void f(int n) {while (lock.test_and_set(std::memory_order_acquire)) { //…

JDBC連接(MySql)數據庫步驟,以及查詢、插入、刪除、更新等十一個處理數據庫信息的功能。...

主要內容&#xff1a; JDBC連接數據庫步驟。一個簡單詳細的查詢數據的例子。封裝連接數據庫&#xff0c;釋放數據庫連接方法。實現查詢&#xff0c;插入&#xff0c;刪除&#xff0c;更新等十一個處理數據庫信息的功能。&#xff08;包括事務處理&#xff0c;批量更新等&#x…