挑战程序竞赛系列(76):4.3强连通分量分解(3)

挑战程序竞赛系列(76):4.3强连通分量分解(3)

传送门:POJ 1236: Network of Schools

题意:

校园网:给定N所学校和网络,目标是分发软件其他学校都可收到,求①所需最少分发学校数;②若任选学校都能收到,最低新增边数。

针对第一问,求解缩点后,入度为0的顶点个数。 针对第二问,求解缩点后,入度为0和出度为0的顶点个数的最大值。

注意缩点后的出入度表示方法,其中如果在一个强连通分量中,则不进行统计。

代码如下:

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/P1236.txt";

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

    static final int MAX_V = 100 + 2;
    List<Edge>[] g, rg;
    List<Integer> po;
    boolean[] used;
    int[] cmp;
    int V;

    void init(int V) {
        this.V = V;
        g  = new ArrayList[MAX_V];
        rg = new ArrayList[MAX_V];
        po = new ArrayList<Integer>();
        used = new boolean[MAX_V];
        cmp  = new int[MAX_V];
        for (int i = 0; i < V; ++i) g[i]  = new ArrayList<Edge>();
        for (int i = 0; i < V; ++i) rg[i] = new ArrayList<Edge>();
    }

    void add(int from, int to) {
        g[from].add(new Edge(from, to));
        rg[to].add(new Edge(to, from));
    }

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

    void rdfs(int v, int k) {
        used[v] = true;
        cmp[v]  = k;
        for (Edge e : rg[v]) {
            if (!used[e.to]) rdfs(e.to, 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) {
            if (!used[po.get(i)]) rdfs(po.get(i), k++);
        }
        return k;
    }

    class Edge{
        int from;
        int to;
        Edge(int from, int to){
            this.from = from;
            this.to   = to;
        }
    }

    void read() {
        int N = ni();
        init(N);
        for (int i = 0; i < N; ++i) {
            while (true) {
                int v = ni();
                if (v == 0) break;
                add(i, v - 1);
            }
        }
        int scc = kosarajuSCC();
        if (scc == 1) {
            out.println("1\n0");
            return;
        }

        int[] in = new int[scc];
        int[] oo = new int[scc];
        for (int v = 0; v < V; ++v) {
            for (Edge e : g[v]) {
                int to = e.to;
                if (cmp[v] != cmp[to]) { //连通分量中的点不统计
                    ++in[cmp[to]];
                    ++oo[cmp[v]];
                }
            }
        }
        int l = 0, r = 0;
        for (int i = 0; i < scc; ++i) {
            if (in[i] == 0) l ++;
            if (oo[i] == 0) r ++;
        }

        out.println(l);
        out.println(Math.max(l, r));
    }

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏机器学习入门

挑战程序竞赛系列(91):3.6凸包(2)

挑战程序竞赛系列(91):3.6凸包(2) 传送门:POJ 1113: Wall 题意参考hankcs: http://www.hankcs.com/pro...

1916
来自专栏机器学习入门

挑战程序竞赛系列(75):4.3强连通分量分解(2)

挑战程序竞赛系列(75):4.3强连通分量分解(2) 传送门:POJ 3180: The Cow Prom 题意: 求结点大于等于2的强连通分量个数。 直接上...

1886
来自专栏计算机视觉与深度学习基础

Leetcode 284. Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design a...

2803
来自专栏闵开慧

eclipse中运行hbase时不能显示表中的值

for(Result row:table.getScanner("fam1".getBytes())){ // System.out.for...

3348
来自专栏JavaQ

RPC框架的最简单实现

一个最简单的RPC框架实现包含暴露服务、引用服务、Socket通讯三部分。 RPC框架简易实现 public class RpcFramework { ...

3104
来自专栏机器学习入门

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

挑战程序竞赛系列(78):4.3 2-SAT(2) 传送门:POJ 3678: Katu Puzzle 题意: 某组合电路有N个输入,M个与或异或门将其两两...

1966
来自专栏机器学习入门

挑战程序竞赛系列(92):3.6凸包(3)

挑战程序竞赛系列(92):3.6凸包(3) 传送门:POJ 1912: A highway and the seven dwarfs 题意: 高铁与七个小矮人...

1569
来自专栏JAVA技术站

JAVA流之ObjectOutputStream,ObjectInputStream

894
来自专栏机器学习入门

挑战程序竞赛系列(93):3.6凸包(4)

挑战程序竞赛系列(93):3.6凸包(4) 传送门:POJ 3608: Bridge Across Islands 题意: 跨岛大桥:在两个凸包小岛之间造桥,...

1966
来自专栏机器学习入门

挑战程序竞赛系列(90):3.6凸包(1)

挑战程序竞赛系列(90):3.6凸包(1) 传送门:POJ 2187: Beauty Contest 题意: 平面上有N个牧场。i号牧场的位置在格点(xi,y...

2007

扫码关注云+社区