图的割点

图的割点

自己记录的细节,和自身遇到的问题

public class 图的割点 {
    static int n;
    static int m;
    static int index;
    static int[][] a = new int[10][10];
    static int[] num = new int[10];
    static int[] low = new int[10];
    static int[] flag = new int[10];
    static int root = 1;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        int t1;
        int t2;
        for (int i = 0; i < m; i++) {
            t1 = sc.nextInt();
            t2 = sc.nextInt();
            a[t1][t2] = 1;
            a[t2][t1] = 1;
        }
        dfs(1,root);
        for (int i = 1; i <= n; i++) {
            if(flag[i] == 1)
                System.out.println(i);
        }
    }
    public static void dfs(int s,int f){
        int child = 0;
        index++;
        num[s] = index;      //s是被第index个访问的
        low[s] = index;     //low[s]是在不经过父节点的情况下,dfs最小的num[s]
        for (int i = 1; i <= n; i++) {
            if(a[s][i] == 1){
                if(num[i] == 0){
                    child++;
                    dfs(i,s);        //前提是i这个点没有被访问过,没有经过(num[i] == 0),才执行dfs
                    low[s] = Math.min(low[s],low[i]);
                }
                else if(i != f){
                    low[s] = Math.min(low[s],num[i]); //为什么是num[i]而不能是low[i]呢?因为i的搜索路径可能包含了s的父节点
                }
                if(s != root && low[i] >= num[s])
                    flag[s] = 1;
                //如果子节点大于1,说明第一个子节点深度优先搜索到不了第二个子节点,第一个子节点和第二个子节点之间没有通路
                else if(s == root && child > 1)
                    flag[s] = 1;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值