首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战程序竞赛系列(78):4.3 2-SAT(2)

挑战程序竞赛系列(78):4.3 2-SAT(2)

作者头像
用户1147447
发布2018-01-02 10:58:26
5150
发布2018-01-02 10:58:26
举报

挑战程序竞赛系列(78):4.3 2-SAT(2)

传送门:POJ 3678: Katu Puzzle

题意:

某组合电路有N个输入,M个与或异或门将其两两相连构成多个输出,问是否存在满足给定输出的输入。

思路: 2-SAT模型都是解题套路,对于每一条边,如 0 1 1 AND:

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

给定c = 1,说明 0-0,0-1,1-0的关系矛盾,据此加边:
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);

给定c = 0,说明 1-1关系矛盾,据此加边:
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.InputStreamReader;
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) {
            g[from].add(to);
            rg[to].add(from);
        }

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

        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;
        }

    }

    void read() {
        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();
        read();
        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 {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } 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);
        }
    }
}
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(78):4.3 2-SAT(2)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档