题意:给出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. Linux tar 解压缩命令

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  2. wpf鼠标捕获与控件交互——UIElement.CaptureMouse

    应用场景是这样的,我需要拖动一个元素在屏幕上移动,注册了被移动元素的MouseMove事件,但是当鼠标移到被移动元素的外面时,移动失效,且鼠标的手势变成了普通的箭头形状,于是就找到了以下的解决方案. ...

  3. P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

    裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...

  4. EF4.1之覆盖EF的默认的约定

    覆盖EF默认的约定可以通过两种方式: 1.拦截模型构建器,使用流畅的API 2.通过给 类添加标签 好的,我还用之前定义的订单类来做例子: public class Order { public in ...

  5. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  6. HTML5 中的块级链接

    英文叫做 “Block-level” links,我以为只有我厂那些鸟毛不知道,没想到不知道的还挺多, 需要普及一下. 最近看了 kejun 的 PPT 前端开发理论热点面对面:从怎么看,到怎么做?, ...

  7. The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971 题意:将输入的英文数字表达转化为阿拉伯数字. #include< ...

  8. XSS与CSRF两种跨站攻击比较

    XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery) 在那个年代,大家一般用拼接字符串的方式来构造动态SQL 语 ...

  9. 【leetcode】Word Ladder (hard) ★

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  10. *[topcoder]GooseTattarrattatDiv1

    http://community.topcoder.com/stat?c=problem_statement&pm=12730&rd=15701 这道题有点意思.首先把字符串变成回文, ...