题目地址:

https://www.lintcode.com/problem/tree/description

给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树)。要求应对若干次询问,每次询问问两个顶点之间是父子关系还是兄弟关系,如果是父子关系则记为222,兄弟关系记为111,否则为000。父子关系的意思是,其中一个顶点是另一个的父亲节点;兄弟关系的意思是两个顶点有相同的父亲。

对图进行BFS,令用一个哈希表记录每个顶点的父亲节点的编号。预处理完毕后开始应对询问,每次询问的时候求一下两个顶点的父亲,如果一个是另一个的父亲则记222,如果两个顶点父亲相等则记111,否则记000。代码如下:

import java.util.*;public class Solution {/*** @param x: The x* @param y: The y* @param a: The a* @param b: The b* @return: The Answer*/public int[] tree(int[] x, int[] y, int[] a, int[] b) {// Write your code hereMap<Integer, Set<Integer>> graph = buildGraph(x, y);Map<Integer, Integer> parent = new HashMap<>();Queue<Integer> queue = new LinkedList<>();queue.offer(1);Set<Integer> visited = new HashSet<>();visited.add(1);while (!queue.isEmpty()) {int cur = queue.poll();for (int next : graph.get(cur)) {if (visited.contains(next)) {continue;}// 记next的父亲是curparent.put(next, cur);queue.offer(next);visited.add(next);}}int[] res = new int[a.length];for (int i = 0; i < a.length; i++) {int p1 = a[i], p2 = b[i];// 用getOrDefault的目的是为了防止p1和p2其中有一个是1,而1是没有父亲的;// 如果两者父亲相等则说明是兄弟,如果其中一个父亲是另一个则说明是父子if (parent.getOrDefault(p1, -1) == parent.getOrDefault(p2, -1)) {res[i] = 1;} else if (parent.getOrDefault(p1, -1) == p2 || parent.getOrDefault(p2, -1) == p1) {res[i] = 2;}}return res;}// 邻接表建图private Map<Integer, Set<Integer>> buildGraph(int[] x, int[] y) {Map<Integer, Set<Integer>> graph = new HashMap<>();for (int i = 0; i < x.length; i++) {graph.computeIfAbsent(x[i], k -> new HashSet<>()).add(y[i]);graph.computeIfAbsent(y[i], k -> new HashSet<>()).add(x[i]);}return graph;}
}

时空复杂度O(V)O(V)O(V)。

【Lintcode】1413. Tree相关推荐

  1. 【HDU】5370 Tree Maker 【树dp】

    传送门:[HDU]5370 Tree Maker my  code:my~~code: #include <bits/stdc++.h> using namespace std ;type ...

  2. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  3. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  4. 【Lintcode】444. Graph Valid Tree II

    题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...

  5. 【Lintcode】1533. N-ary Tree Level Order Traversal

    题目地址: https://www.lintcode.com/problem/n-ary-tree-level-order-traversal/description 分层遍历一棵多叉树.题目中的li ...

  6. 【Lintcode】1495. Leaf-Similar Trees

    题目地址: https://www.lintcode.com/problem/leaf-similar-trees/description 定义一棵二叉树的"树叶序列",为其所有叶 ...

  7. 【解析】UVA-548 Tree

    立志用最少的代码做最高效的表达 You are to determine the value of the leaf node in a given binary tree that is the t ...

  8. html树 node节点定位,【Vue】element-ui Tree如何定位到一个节点,并高亮显示该节点?...

    我要实现的效果是:搜索关键字,得到搜索结果 点击搜索结果,展开节点,并定位到该结果的位置(高亮显示) 目前只实现了展开节点的效果 定位到该结果的位置和高亮显示这两点无法实现 定位到该结果的位置:目前只 ...

  9. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

最新文章

  1. 2021年大数据Spark(二十七):SparkSQL案例一花式查询和案例二WordCount
  2. MySQL 高级 - 语法 - if判断
  3. 洛谷 - P3690 【模板】Link Cut Tree (动态树)(LCT模板)
  4. MyEclipse连接MySQL的方法
  5. python查看CNN训练模型参数
  6. vcm服务器如何修改端口,VCM2000是迈普协同通信解决方案的网管服务器.doc
  7. jdbc连接oracle_Oracle数据库性能监控|使用SiteScope 监控Oracle
  8. pycharm退出测试环境
  9. unity3d 改变脚本名称_Unity3D脚本中文教程
  10. 麒麟服务器数据库协议,麒麟服务器神通数据库安装
  11. 浙大翁凯老师Java课堂学习记录(第二周)
  12. P2P网贷平台风险分析报告
  13. 电容或电感的电压_用动画来解释电感和电容元件上电压电流超前滞后的关系
  14. volatility内存取证分析与讲解(持续更新)
  15. unity 粒子系统面板参数释义
  16. 后AlphaFold时代的蛋白质结构预测
  17. 韦根w34是多少位_Levi's裤子尺码中的W34和L34各是多少厘米?
  18. python的字节码(ByteCode)
  19. 【前言】 VVC理论知识之基本框架
  20. SQL Server从入门到精通(转)

热门文章

  1. 电路维修知识-可控硅
  2. (自兴人工智能)python元组
  3. 【CSAPP】计算机系统漫游
  4. 用友通T3联不上服务器
  5. 中国广电明年推进5G覆盖乡镇,用户:和中国移动相比信号有何优势?
  6. 图纸上标注的是实际尺寸吗_CAD比例画图时,图上标的尺寸,是实际尺寸还是图上尺寸啊?...
  7. C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十四) Be careful!前方怪物出没...
  8. 常用的第三方SDK介绍(搜集中)
  9. 听说这两款是最适合程序员编程的电脑
  10. 帆软连接好数据库,字段带有中文的显示乱码解决方案