# 挑战程序竞赛系列（78）：4.3 2-SAT（2）

## 挑战程序竞赛系列（78）：4.3 2-SAT（2）

```AND
a b
0 0 0
0 1 0
1 0 0
1 1 1

2 * a， 2 * b 等价于 0
2 * a + 1 , 2 * b + 1等价于 1

scc.add(2 * a, 2 * b + 1);
scc.add(2 * b, 2 * a + 1);
scc.add(2 * a, 2 * b);
scc.add(2 * b + 1, 2 * a + 1);
scc.add(2 * a + 1, 2 * b + 1);
scc.add(2 * b, 2 * a);

scc.add(2 * a + 1, 2 * b);
scc.add(2 * b + 1, 2 * a);

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Main{

String INPUT = "./data/judge/201709/P3678.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

class SCC {
static final int MAX_V = 1000 + 8;
List<Integer>[] g  = new List[MAX_V];
List<Integer>[] rg = new List[MAX_V];
List<Integer> po   = new ArrayList<Integer>();
boolean[] used = new boolean[MAX_V];
int[] cmp = new int[MAX_V];
int V;

SCC(int V) {
this.V = V;
po = new ArrayList<Integer>();
Arrays.fill(used, false);
for (int i = 0; i < V; ++i) g[i]  = new ArrayList<Integer>();
for (int i = 0; i < V; ++i) rg[i] = new ArrayList<Integer>();
}

void add(int from, int to) {
}

void dfs(int v) {
used[v] = true;
for (int u : g[v]) {
if (!used[u]) dfs(u);
}
}

void rdfs(int v, int k) {
used[v] = true;
cmp[v]  = k;
for (int u : rg[v]) {
if (!used[u]) rdfs(u, k);
}
}

int kosarajuSCC() {
for (int v = 0; v < V; ++v) {
if (!used[v]) dfs(v);
}
Arrays.fill(used, false);
int k = 0;
for (int i = po.size() - 1; i >= 0; --i) {
int v = po.get(i);
if (!used[v]) rdfs(v, k++);
}
return k;
}

}

int N = ni();
int M = ni();
SCC scc = new SCC(N * 2);
for (int i = 0; i < M; ++i) {
int a = ni();
int b = ni();
int c = ni();
String op = ns();
if (op.equals("AND")) {
if (c == 0) {
scc.add(2 * a + 1, 2 * b);
scc.add(2 * b + 1, 2 * a);
}
else {
scc.add(2 * a, 2 * b + 1);
scc.add(2 * b, 2 * a + 1);
scc.add(2 * a, 2 * b);
scc.add(2 * b + 1, 2 * a + 1);
scc.add(2 * a + 1, 2 * b + 1);
scc.add(2 * b, 2 * a);
}
}
else if (op.equals("OR")) {
if (c == 0) {
scc.add(2 * a, 2 * b);
scc.add(2 * b + 1, 2 * a + 1);
scc.add(2 * a + 1, 2 * b + 1);
scc.add(2 * b, 2 * a);
scc.add(2 * a + 1, 2 * b);
scc.add(2 * b + 1, 2 * a);
}
else {
scc.add(2 * a, 2 * b + 1);
scc.add(2 * b, 2 * a + 1);
}
}
else {
if (c == 0) {
scc.add(2 * a, 2 * b);
scc.add(2 * b + 1, 2 * a + 1);
scc.add(2 * a + 1, 2 * b + 1);
scc.add(2 * b, 2 * a);
}
else {
scc.add(2 * a, 2 * b + 1);
scc.add(2 * b, 2 * a + 1);
scc.add(2 * a + 1, 2 * b);
scc.add(2 * b + 1, 2 * a);
}
}
}

scc.kosarajuSCC();
for (int i = 0; i < N; ++i) {
if (scc.cmp[2 * i] == scc.cmp[2 * i + 1]) {
out.println("NO");
return;
}
}
out.println("YES");
}

FastScanner in;
PrintWriter out;

void run() throws IOException {
boolean oj;
try {
oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
} catch (Exception e) {
oj = System.getProperty("ONLINE_JUDGE") != null;
}

InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
in = new FastScanner(is);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
out.flush();
if (!oj){
System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
}
}

public boolean more(){
return in.hasNext();
}

public int ni(){
return in.nextInt();
}

public long nl(){
return in.nextLong();
}

public double nd(){
return in.nextDouble();
}

public String ns(){
return in.nextString();
}

public char nc(){
return in.nextChar();
}

class FastScanner {
StringTokenizer st;
boolean hasNext;

public FastScanner(InputStream is) throws IOException {
hasNext = true;
}

public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} catch (Exception e) {
hasNext = false;
return "##";
}
}
return st.nextToken();
}

String next = null;
public boolean hasNext(){
next = nextToken();
return hasNext;
}

public int nextInt() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Integer.parseInt(more);
}

public long nextLong() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Long.parseLong(more);
}

public double nextDouble() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Double.parseDouble(more);
}

public String nextString(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more;
}

public char nextChar(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more.charAt(0);
}
}
}```

0 条评论

## 相关文章

### C# 对象哈希码

FCL的设计者认为,如果能将任何对象的任何实例放到哈希集合中,能带来很多好处。为此,System.Object提供了GetHashCode,它能获取任何对象的I...

1845

2776

2119

### C#基础知识系列十(集合)

本节主要是来了解学习集合，以方便在程序编写时，什么地方该选用什么集合，让程序更健壮的运行起来。在学习了解集合之前，首先需要了解一些数据结构方面的知识。下面我...

933

2060

### [C#1] 8-数组

1.数组概述 声明数组： //每个元素初始化为0，虽然数组元素是值类型，但是却是分配在托管堆中的； int[] myArray=new int[100]; //...

1797

19210

### Java的算数运算符、关系运算符、逻辑运算符、位运算符

b=5,a=5 c=-5,a=5 d=3,l=3 f=3,m=4 g=3,n=3 h=6,o=5

892

### POJ 刷题系列：2262. Goldbach's Conjecture

POJ 刷题系列：2262. Goldbach’s Conjecture 传送门：POJ 2262. Goldbach’s Conjecture 题意： 给定...

1526

1896