码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java类似stl建树代码(ACM)


    java实现stl建树的方法,就是类似于c++那样,创建一个list数组,将元素压入到指定的list[x]中

    在dfs函数中可以进行调用,实现遍历这个树

    list数组建树:

    1. import java.util.*;
    2. public class Main{
    3. static List[] list=new ArrayList[100005];
    4. public static void main(String[] args) {
    5. Scanner cin=new Scanner(System.in);
    6. int n=cin.nextInt();
    7. for(int i=0;i<=n;i++) list[i]=new ArrayList();
    8. for(int i=1;i<n;i++){
    9. int x=cin.nextInt();
    10. int y=cin.nextInt();
    11. list[x].add(y);list[y].add(x); //两边压入
    12. }
    13. dfs(1,0);
    14. }
    15. static void dfs(int d,int pre){ //d是顶点,pre是父点
    16. System.out.println(d);
    17. int l=list[d].size();
    18. for(int i=0;i<l;i++){
    19. int son=(int)list[d].get(i);
    20. if(son!=pre){ //不为父点则进入
    21. dfs(son,d);
    22. }
    23. }
    24. }
    25. }
    26. /*
    27. 4
    28. 1 2
    29. 2 3
    30. 2 4
    31. */

    同c++,二维list并不能很好实现树的建立,需要提前压入多个list后才可。

    但是java的二维list可能真不行,因为非数组形式的二维list无法继续压入

    即数组形式可实现:list[x].add(10)

    二维形式要这样:list.get(x).add(10)

    可以看到get函数返回的不是可改的对象,是一个仅可读的对象,没有listx]的指针不能进行压入。


    java建树的方法还有map建树:

    主要就是类似于c++的map<int,vector<int>> mp;去映射一个序列,没有上面好用仅做了解即可

    1. map = new HashMap<>();
    2. for(int i = 1; i <= N; i++) map.put(i, new ArrayList<>()); //指定下标压入一个序列

    1. import java.util.*;
    2. //某树形DP题代码
    3. public class Main{
    4. static int[] h;
    5. static Map<Integer, List<Integer>> map;
    6. static int[][] f;
    7. public static void main(String[] args){
    8. Scanner sc = new Scanner(System.in);
    9. int N = sc.nextInt();
    10. h = new int[N + 1];
    11. map = new HashMap<>();
    12. int root = (N + 1) * N / 2;
    13. for(int i = 1; i <= N; i++) map.put(i, new ArrayList<>());
    14. for(int i = 1; i <= N; i++) h[i] = sc.nextInt();
    15. for(int i = 1; i <= N - 1; i++){
    16. int l = sc.nextInt(), k = sc.nextInt();
    17. root -= l;
    18. map.get(k).add(l);
    19. }
    20. f = new int[N + 1][2];
    21. dfs(root);
    22. System.out.println(Math.max(f[root][0], f[root][1]));
    23. }
    24. static void dfs(int x){
    25. f[x][1] = h[x];
    26. for(int son: map.get(x)){
    27. dfs(son);
    28. f[x][0] += Math.max(f[son][0], f[son][1]);
    29. f[x][1] += f[son][0];
    30. }
    31. }
    32. }

  • 相关阅读:
    代码随想录打卡第五十三天|309.最佳买卖股票时机含冷冻期 ● 714.买卖股票的最佳时机含手续费
    数据结构和算法(12):词典
    C语言自定义类型详解(1)结构体知识汇总
    千兆以太网网络层 ARP 协议的原理与 FPGA 实现
    vue2学习之路由(router以及vue-router)
    rt-thread + lvgl :线程调用lv_task_handler() 之后,无法休眠
    Spring底层原理学习笔记--第四讲--(常见bean后处理器与@Autowired bean后处理器执行分析)
    一维数组——找公共元素
    Android BLE 蓝牙开发——扫码枪基于BLESSED
    前端架构师之10_JavaScript_DOM
  • 原文地址:https://blog.csdn.net/m0_58177653/article/details/125431744
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号