【算法基础:搜索与图论】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

相关推荐
Tiandaren几秒前
Selenium 4 教程:自动化 WebDriver 管理与 Cookie 提取 || 用于解决chromedriver版本不匹配问题
selenium·测试工具·算法·自动化
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说3 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表
卡卡卡卡罗特5 小时前
每日mysql
数据结构·算法
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
lifallen7 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest7 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
EndingCoder7 小时前
搜索算法在前端的实践
前端·算法·性能优化·状态模式·搜索算法