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

2018-01-02 10:57:13 浏览数 (1)

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

传送门:POJ 2723: Get Luffy Out

题意:

题目意思有点坑,实际上给出每一对钥匙,如(0,3),如果选择了钥匙0,那么后续的门只能用钥匙0开,而不能再选择3,意味着每对钥匙是可以开多扇门的。

思路: 二分 2-SAT,每一扇门有两把锁,一扇门能推出一对矛盾关系,如OJ上提供的数据:

代码语言:javascript复制
3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0

第一扇门为:(0,1)
那么对应的钥匙串有(0,3)和(1,2)

开任意一把锁都能通过该层,如下:
钥匙串A 钥匙串B  门
0      1       通过
0      2       通过
3      1       通过
3      2       未通过

所以此处矛盾关系为3和2,即钥匙串1和钥匙串2不能同时选择3和2。
有了矛盾关系就可以根据2-SAT模型去解了。

二分:
显然,矛盾关系越多(每扇门一个约束条件),符合条件的钥匙串选择就越少,符合单调性,加快搜索。

代码如下:

代码语言:javascript复制
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.Map;
import java.util.StringTokenizer;

public class Main{

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

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

    int[][] doors;
    int[] keys;
    int N, M;

    class SCC {
        int V;
        List<Integer>[] g;
        List<Integer>[] rg;
        List<Integer> po;
        boolean[] used;
        int[] cmp;

        SCC(int V){
            this.V = V;
            g  = new List[V];
            rg = new List[V];
            po = new ArrayList<Integer>();
            used = new boolean[V];
            cmp  = new int[V];
            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 i = 0; i < V;   i) {
                if (!used[i]) dfs(i);
            }
            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() {
        while (true) {
            N = ni();
            M = ni();
            if (N   M == 0) break;

            keys = new int[2 * N];
            for (int i = 0; i < N;   i) {
                int A = ni();
                int B = ni();
                keys[A] = B;
                keys[B] = A;
            }

            doors = new int[M][2];
            for (int i = 0; i < M;   i) {
                doors[i] = new int[] {ni(), ni()};
            }

            solve();
        }
    }

    void solve() {
        int lf = 0, rt = M;
        while (lf < rt) {
            int mid = lf   (rt - lf   1) / 2;
            if (!valid(mid)) {
                rt = mid - 1;
            }
            else {
                lf = mid;
            }
        }
        out.println(lf);
    }

    boolean valid(int m) {
        SCC scc = new SCC(2 * N);
        for (int i = 0; i < m;   i) {
            scc.add(keys[doors[i][0]], doors[i][1]);
            scc.add(keys[doors[i][1]], doors[i][0]);
        }

        scc.kosarajuSCC();
        for (int i = 0; i < scc.V;   i) {
            if (scc.cmp[i] == scc.cmp[keys[i]]) {
                return false;
            }
        }
        return true;
    }

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

    static class D{

        public static void pp(int[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row;   i) {
                for (int j = 0; j < col;   j) {
                    sb.append(board[i][j]   (j   1 == col ? "n" : " "));
                }
            }
            System.out.println(sb.toString());
        }

        public static void pp(char[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row;   i) {
                for (int j = 0; j < col;   j) {
                    sb.append(board[i][j]   (j   1 == col ? "n" : " "));
                }
            }
            System.out.println(sb.toString());
        }
    }

    static class ArrayUtils {

        public static void fill(int[][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                Arrays.fill(f[i], value);
            }
        }

        public static void fill(int[][][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                fill(f[i], value);
            }
        }

        public static void fill(int[][][][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                fill(f[i], value);
            }
        }
    }

    static class Num{
        public static <K> void inc(Map<K, Integer> mem, K k) {
            if (!mem.containsKey(k)) mem.put(k, 0);
            mem.put(k, mem.get(k)   1);
        }
    }
}
get

0 人点赞