【算法基础:搜索与图论】3.2 树与图的dfs和bfs

要学会建树、建图的通用方法。

dfs 和 bfs 的代码框架。

例题

846. 树的重心(深度优先遍历 / 树形DP)⭐⭐⭐⭐⭐🚹🚹🚹🚹🚹(重要!好题!)

https://www.acwing.com/problem/content/848/
在这里插入图片描述

在 dfs 的过程中,统计各个节点作为断点时的连通块最大值。

import java.util.*;

public class Main {
    static List<Integer>[] g;
    static int ans = Integer.MAX_VALUE, n = 0;

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        g = new ArrayList[n + 1];
        Arrays.setAll(g, e -> new ArrayList<Integer>());
        for (int i = 0; i < n - 1; ++i) {
            int a = scanner.nextInt(), b = scanner.nextInt();
            g[a].add(b);
            g[b].add(a);
        }

        dfs(1, 0);      // 为什么要用dfs?因为dfs可以求出子树的大小
        System.out.println(ans);
    }

    static int dfs(int x, int fa) {
        // res记录删除该节点之后各个连通块中点数的最大值;sum记录该节点为根节点时所在连通块点的数量
        int res = 0, sum = 1;
        for (int y: g[x]) {
            if (y != fa) {
                int s = dfs(y, x);      // 枚举各个x的子节点y作为根节点时的连通块大小
                res = Math.max(res, s);
                sum += s;
            }
        }
        res = Math.max(res, n - sum);	// n - sum 是 x 的父节点以上的那个连通块的大小
        ans = Math.min(ans, res);
        return sum;
    }
}

847. 图中点的层次

https://www.acwing.com/problem/content/849/

在这里插入图片描述

看到最短距离就可以想到使用宽搜。

注意! :题目中说明了 a 和 b 表示存在一条从 a 走到 b 的边,即这是一个有向图。在建图的过程中,对于每一行只需要添加一条路径就可以了。

import java.util.*;

public class Main {
    static List<Integer>[] g;
    static int n, m;

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        g = new ArrayList[n + 1];
        Arrays.setAll(g, e -> new ArrayList<Integer>());
        for (int i = 0; i < m; ++i) {
            int a = scanner.nextInt(), b = scanner.nextInt();
            g[a].add(b);		// 只需要添加从 a 走到 b 的路径
        }
        System.out.println(bfs());
    }

    static int bfs() {
        Queue<Integer> q = new LinkedList<>();
        boolean[] st = new boolean[n + 1];
        q.offer(1);
        st[1] = true;
        int ans = 0;

        while (!q.isEmpty()) {
            int sz = q.size();
            for (int i = 0; i < sz; ++i) {
                int x = q.poll();
                if (x == n) return ans;
                for (int y: g[x]) {
                    if (!st[y]) {
                        st[y] = true;
                        q.offer(y);
                    }
                }
            }
            ++ans;
        }
        return -1;
    }
}

相关链接

更多关于树形 DP 可见:
【算法】树形DP ①(树的直径)
【算法】树形DP ② 打家劫舍Ⅲ(树上最大独立集)
【算法】换根DP

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wei *

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值