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

文章目录

  • 例题
    • [846. 树的重心(深度优先遍历 / 树形DP)⭐⭐⭐⭐⭐🚹🚹🚹🚹🚹(重要!好题!)](#846. 树的重心(深度优先遍历 / 树形DP)⭐⭐⭐⭐⭐🚹🚹🚹🚹🚹(重要!好题!))
    • [847. 图中点的层次](#847. 图中点的层次)
  • 相关链接

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

dfs 和 bfs 的代码框架。

例题

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

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

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

java 复制代码
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 的边,即这是一个有向图。在建图的过程中,对于每一行只需要添加一条路径就可以了。

java 复制代码
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

相关推荐
草莓熊Lotso20 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM26 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
CV点灯大师41 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
GGBondlctrl1 小时前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
武子康1 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
武子康1 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
超的小宝贝3 小时前
数据结构算法(C语言)
c语言·数据结构·算法
木子.李3479 小时前
排序算法总结(C++)
c++·算法·排序算法