题意:有n个顶点里面可以放数字1或0,给m个限制,每个限制给出两个顶点编号和两编号内数字运算后的结果

思路:很直接的2-SAT,每个点分为1和0两种情况,按限制要求建边,跑tarjan然后判断点是否在同一个强连通分量里就OK了

(一下代码是WA的。。找了一晚找不到BUG)

 #include<cstdio>
const int maxn = 1e3+;
int stack[maxn],dfn[maxn<<],low[maxn<<],head[maxn*maxn],dfs_num,top;
int color[maxn<<],col_num;
bool vis[maxn*maxn];
class edge
{
public:
int to,next;
}e[];
inline int gmin(int a,int b)
{
return a<b?a:b;
}
int ans;
void addedge(int u,int v)
{
e[++ans].next=head[u];
e[ans].to=v;
head[u]=ans;
}
void Tarjan ( int x ) {
dfn[ x ] = ++dfs_num ;
low[ x ] = dfs_num ;
vis [ x ] = true ;//是否在栈中
stack [ ++top ] = x ;
for ( int i=head[ x ] ; i!= ; i=e[i].next ){
int temp = e[ i ].to ;
if ( !dfn[ temp ] ){
Tarjan ( temp ) ;
low[ x ] = gmin ( low[ x ] , low[ temp ] ) ;
}
else if ( vis[ temp ])low[ x ] = gmin ( low[ x ] , dfn[ temp ] ) ;
}
if ( dfn[ x ]==low[ x ] ) {//构成强连通分量
vis[ x ] = false ;
color[ x ] = ++col_num ;//染色
while ( stack[ top ] != x ) {//清空
color [stack[ top ]] = col_num ;
vis [ stack[ top-- ] ] = false ;
}
top -- ;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int x,y,z;
char s[];
scanf("%d%d%d%s",&x,&y,&z,s);
if(s=="AND")
{if(z==)addedge(*x+,*x),addedge(*y+,*y);
else addedge(*x,*y+),addedge(*y,*x+);
}else if(s=="OR")
{if(z==)addedge(*x+,*y),addedge(*y+,*x);
else addedge(*x,*x+),addedge(*y,*y+);
}else{
if(z==)addedge(*x,*y+),addedge(*y+,*x),addedge(*y,*x+),addedge(*x+,*y);
else addedge(*x,*y),addedge(*y,*x),addedge(*x+,*y+),addedge(*y+,*x+);
}
} for(int i=;i<*n;i++)
if(!dfn[i])Tarjan(i);
for(int i=;i<n;i++)
if(color[*i]==color[*i+]){printf("NO\n");return ;}
printf("YES\n");
return ;
}

POJ 3678 2-SAT的更多相关文章

  1. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

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

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

  3. poj 3678 Katu Puzzle(Two Sat)

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

  4. 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 ...

  5. Katu Puzzle POJ - 3678(水2 - sat)

    题意: 有n个未知量,m对未知量之间的关系,判断是否能求出所有的未知量且满足这些关系 解析: 关系建边就好了 #include <iostream> #include <cstdio ...

  6. Katu Puzzle POJ - 3678 (2 - sat)

    有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...

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

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

  8. 基础但是很重要的2-sat POJ 3678

    http://poj.org/problem?id=3678 题目大意:就是给你n个点,m条边,每个点都可以取值为0或者1,边上都会有一个符号op(op=xor or and三种)和一个权值c.然后问 ...

  9. 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 ...

  10. poj 3678(SCC+2-SAT)

    传送门:Problem 3678 https://www.cnblogs.com/violet-acmer/p/9769406.html 难点: 题意理解+构图 题意: 有n个点 v[0,2..... ...

随机推荐

  1. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  2. Linux中的错误重定向你真的懂吗

    在很多定时任务里.shell里我们往往能看到 "2>&1",却不知道这背后的原理. 举个例子: * 1 * * * test.sh > /dev/null 2& ...

  3. Java基础二(2020.1.14)

    学习内容: 1.Java运算符:赋值运算符,算术运算符,关系运算符,逻辑运算符,条件运算符 2.java流程控制:顺序,选择 1.java输入 Scanner s = new Scanner(Syst ...

  4. 1.docker 的 安装

    1.mac 安装docker 1.1 打开 https://docs.docker.com/docker-for-mac/install/ 1.2 下载 dmg 文件 1.3  打开 docker,d ...

  5. linux(centos 7)安装及使用yum

    yum介绍: Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的 ...

  6. 6)PHP,预定义变量

    预定义变量也叫超全局变量: :预定义变量又叫超全局变量,包括: $_GET, $_POST, $_SERVER, $_REQUEST, $GLOBALS, $_COOKIE, $_SESSION, . ...

  7. Maven打包时报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:解决方案

    问题现象: 用Maven打包时,报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war错误. 原因分析: 打 ...

  8. SQLite-外键约束/表链接查询

    外键约束: 表一的某个字段关联到表二的某个字段 例子: 国家表:t_country

  9. Python基础——类new方法与单例模式

    介绍: new方法是类中魔术方法之一,他的作用是给类实例化开辟一个内存地址,并返回一个实例化,再由__init__对这个实例进行初始化,故它的执行肯定就是在初始化方法__init__之前了.new方法 ...

  10. C实现日志等级控制

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h&g ...