第三章图论(十一)

无向图的双连通分量

例题:395. 冗余路径
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 5010;
    static int M = 20010;
    static int n;
    static int m;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] dfn = new int[N];
    static int[] low = new int[N];
    static int timestamp;
    static int[] stk = new int[N];
    static int top;
    static int[] id = new int[N];
    static int dcc_cnt;
    static boolean[] is_bridge = new boolean[M];
    static int[] d = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        Arrays.fill(h, -1);
        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            add(a, b);
            add(b, a);
        }

        tarjan(1, -1);
        for (int i = 0; i < idx; i++) {
            if (is_bridge[i]) {
                d[id[e[i]]]++;
            }
        }
        int cnt = 0;
        for (int i = 1; i <= dcc_cnt; i++) {
            if (d[i] == 1) {
                cnt++;
            }
        }
        System.out.println((cnt + 1) / 2);
    }

    private static void tarjan(int u, int from) {
        dfn[u] = ++timestamp;
        low[u] = dfn[u];
        stk[++top] = u;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if (dfn[j] == 0) {
                tarjan(j, i);
                low[u] = Math.min(low[u], low[j]);
                if (dfn[u] < low[j]) {
                    is_bridge[i] = true;
                    is_bridge[i ^ 1] = true;
                }
            } else if (i != (from ^ 1)) {
                low[u] = Math.min(low[u], dfn[j]);
            }
        }
        if (dfn[u] == low[u]) {
            ++dcc_cnt;
            int y;
            do {
                y = stk[top--];//栈顶元素
                id[y] = dcc_cnt;
            } while (y != u);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

例题:电力
【题目描述】
原题来自:CTU Open 2004
求一个图删除一个点之后,联通块最多有多少。
【输入】
多组数据。第一行两个整数 P,C表示点数和边数。
接下来 C行每行两个整数 p1,p2,表示 p1 与 p2 有边连接,保证无重边。读入以 0结束。
【输出】
输出若干行,表示每组数据的结果。
【输入样例】
3 3
0 1
0 2
2 1
4 2
0 1
2 3
3 1
1 0
0 0
【输出样例】
1
2
2
【提示】
数据范围与提示:
1≤P≤10000,C≥0,0≤p1,p2<P
——————————————————————————————————————————————————

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 10010;
    static int M = 30010;
    static int n;
    static int m;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] dfn = new int[N];
    static int[] low = new int[N];
    static int timestamp;
    static int root;
    static int ans;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            n = sc.nextInt();
            m = sc.nextInt();
            if (n == 0 && m == 0) break;
            Arrays.fill(dfn, 0);
            Arrays.fill(h, -1);
            idx = 0;
            timestamp = 0;
            while (m-- > 0) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                add(a, b);
                add(b, a);
            }
            ans = 0;
            int cnt = 0;
            for (root = 0; root < n; root++) {
                if (dfn[root] == 0) {
                    cnt++;
                    tarjan(root);
                }
            }
            System.out.println(ans + cnt - 1);
        }
    }

    private static void tarjan(int u) {
        dfn[u] = ++timestamp;
        low[u] = dfn[u];
        int cnt = 0;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if (dfn[j] == 0) {
                tarjan(j);
                low[u] = Math.min(low[u], low[j]);
                if (dfn[u] <= low[j]) {
                    cnt++;
                }
            } else {
                low[u] = Math.min(low[u], dfn[j]);
            }
        }
        if (u != root && cnt > 0) {
            cnt++;
        }
        ans = Math.max(ans, cnt);
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

例题:396. 矿场搭建
在这里插入图片描述
最后一个样例没过
输入:
1
2 3
6
1 2
1 3
2 4
2 5
3 6
3 8
0
输出:
Case 1: 3 1
Case 2: 5 1

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 1010;
    static int M = 510;
    static int n;
    static int m;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] dfn = new int[N];
    static int[] low = new int[N];
    static int timestamp;
    static int[] stk = new int[N];
    static int top;
    static int dcc_cnt;
    static ArrayList<Integer>[] dcc = new ArrayList[N];
    static boolean[] cut = new boolean[N];
    static int root;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < dcc.length; i++) {
            dcc[i] = new ArrayList<>();
        }
        int T = 1;
        while (true) {
            m = sc.nextInt();
            if (m == 0) break;
            for (int i = 1; i <= dcc_cnt; i++) {
                dcc[i].clear();
            }
            idx = 0;
            n = 0;
            timestamp = 0;
            top = 0;
            dcc_cnt = 0;
            Arrays.fill(h, -1);
            Arrays.fill(dfn, 0);
            Arrays.fill(cut, false);
            while (m-- > 0) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                n = Math.max(n, a);
                n = Math.max(n, b);
                add(a, b);
                add(b, a);
            }
            for (root = 1; root <= n; root++) {
                if (dfn[root] == 0) {
                    tarjan(root);
                }
            }
            int res = 0;
            long num = 1;
            for (int i = 1; i <= dcc_cnt; i++) {
                int cnt = 0;
                for (int j = 0; j < dcc[i].size(); j++) {
                    if (cut[dcc[i].get(j)]) {
                        cnt++;
                    }
                }
                if (cnt == 0) {
                    res += 2;
                    num *= dcc[i].size() * (dcc[i].size() - 1) / 2;
                } else if (cnt == 1) {
                    res++;
                    num *= dcc[i].size() - 1;
                }
            }
            System.out.println("Case " + (T++) + ": " + res + " " + num);
        }
    }

    private static void tarjan(int u) {
        dfn[u] = ++timestamp;
        low[u] = dfn[u];
        stk[++top] = u;
        if (u == root && h[u] == -1) {
            dcc_cnt++;
            dcc[dcc_cnt].add(u);
            return;
        }
        int cnt = 0;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if (dfn[j] == 0) {
                tarjan(j);
                low[u] = Math.min(low[u], low[j]);
                if (dfn[u] <= low[j]) {
                    cnt++;
                    if (u != root || cnt > 1) cut[u] = true;
                    ++dcc_cnt;
                    int y;
                    do {
                        y = stk[top--];
                        dcc[dcc_cnt].add(y);
                    } while (y != j);
                    dcc[dcc_cnt].add(u);
                }
            } else {
                low[u] = Math.min(low[u], dfn[j]);
            }
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值