题目链接

题意

给定一张图,对于每条边给出一个运算符\((\&,|,\otimes)\)和一个值\(c(0 \le c \le 1)\)。问能否通过给每个点赋上一个值。使得每条边通过指定的运算都能得到指定的值。

思路

\(2-sat\)问题,需要注意的是当两数\(\&\)起来为\(1\)时。必须全部为\(1\),所以就从每个点的\(0\)向\(1\)连边。同理,当两数\(|\)起来为\(0\)时,必须全部为\(0\),所以就从每个点的\(1\)向\(0\)连边。

代码

/*
* @Author: wxyww
* @Date: 2019-04-27 16:42:24
* @Last Modified time: 2019-04-27 17:09:58
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 3000 + 10,M = 5000000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
struct node {
int v,nxt;
}e[M];
int head[N],ejs;
void add(int u,int v) {
e[++ejs].v = v;e[ejs].nxt = head[u];head[u] = ejs;
}
char s[5];
int sta[N],dfn[N],top,coljs,cnt,vis[N],low[N],col[N];
void tarjan(int u) {
dfn[u] = low[u] = ++cnt;
sta[++top] = u;
vis[u] = 1;
for(int i = head[u];i;i = e[i].nxt) {
int v = e[i].v;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(vis[v]) low[u] = min(low[u],low[v]);
}
if(low[u] == dfn[u]) {
++coljs;
do {
int x = sta[top--];
col[x] = coljs;
vis[x] = 0;
}while(sta[top + 1] != u);
}
}
int main() {
int n = read(),m = read();
for(int i = 1;i <= m;++i) {
int u = read() + 1,v = read() + 1,w = read();
scanf("%s",s + 1);
if(s[1] == 'O') {
if(w) add(u,v + n),add(v,u + n);
else add(u + n,u),add(v + n,v);
}
else if(s[1] == 'X') {
if(w) add(u,v + n),add(u + n,v),add(v + n,u),add(v,u + n);
else add(u,v),add(u + n,v + n),add(v,u),add(v + n,u + n);
}
else {
if(w) add(u,u + n),add(v,v + n);
else add(u + n,v),add(v + n,u);
}
}
for(int i = 1;i <= n + n;++i) if(!dfn[i]) tarjan(i);
for(int i = 1;i <= n;++i) {
if(col[i] == col[i + n]) {
puts("NO");return 0;
}
}
puts("YES");
return 0;
}

bzoj3678 Katu Puzzle的更多相关文章

  1. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

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

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

  4. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  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 建图入门

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

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

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

  8. POJ3678 Katu Puzzle 【2-sat】

    题目 Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean ...

  9. POJ 3678 Katu Puzzle

    Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...

随机推荐

  1. ArcPy 重命名拷贝删除图层

    使用Python脚本进行图层的重命名拷贝及删除,并在过程中利用logging进行日志记录. 附上Python代码: # -*- coding: utf-8 -*- # nightroad import ...

  2. 关于Android Studio 3.2 运行应用时提示 “Instant Run requires that the platform corresponding to your target device (Android 7.0 (Nougat)) is installed.” 的说明

    点击"Run",运行App后,Android Studio显示如图1-1界面: 图1-1 这是因为你连接的外部设备(比如Android手机或AVD)的SDK版本在你的电脑上没有安装 ...

  3. 章节十一、1-Junit介绍

    一.Junit是一个开源的测试框架,在selenium的jar包中,不需要单独安装和搭建环境 二.@BeforeClass:当在方法上加了这个注解的话,这个方法会在这个类的第一个test方法之前运行. ...

  4. gradle下载及配置

    windows安装 1.下载地址:http://services.gradle.org/distributions/ 2.下载**-bin.zip,解压即可 配置环境变量:gradle_home:D: ...

  5. python接口自动化-post请求4

    云盘登录实操案例: 代码参考: # coding:utf-8 import requests ''' https的请求相对于http安全级别高,需要验证SSL证书 import urllib3 使用这 ...

  6. Git clone远程目录443:Timed out 问题(go get)

    现象: 在cmd中用go get -u github.com/kataras/iris   ,提示:443:Timed out 于是在 git bash 中   git clone https://g ...

  7. awk删除最后一个字符

    删除最后一个字符 如:1.1.1,  在file文件中: sed '$s/.$//' file 或者: head -c-2  去掉最后一个字符 head -c-3  去掉最后二个字符 head -c- ...

  8. Linux:Day18(下) Bind9

    子域授权:每个域的名称服务器,都是通过其上级名称服务器在解析库中进行授权. 类似根域授权tld: .com IN NS ns1.com. .com IN NS ns1.com. ns1.com IN ...

  9. FAST LOW-RANK APPROXIMATION FOR COVARIANCE MATRICES

    目录 Nystorm method 低秩逼近 矩阵乘法的逼近 Belabbas M A, Wolfe P J. Fast Low-Rank Approximation for Covariance M ...

  10. numpy科学计算库的基础用法,完美抽象多维数组(原创)

    #起别名避免重名 import numpy as np #小技巧:print从外往内看==shape从左往右看 if __name__ == "__main__": print(' ...