LintCode Find the Weak Connected Component in the Directed Graph
原题链接在这里:http://www.lintcode.com/en/problem/find-the-weak-connected-component-in-the-directed-graph/
题目:
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a label and a list of its neighbors. (a connected set of a directed graph is a subgraph in which any two vertices are connected by direct edge path.)
Notice
Sort the element in the set in increasing order
Given graph:
A----->B C\ | | \ | |\ | |\ v v->D E <- F
Return {A,B,D}, {C,E,F}
. Since there are two connected component which are {A,B,D} and {C,E,F}
题解:
是Union Find类题目. 用HashMap 的key -> value对应关系来维护child -> parent关系.
对于每一组node -> neighbor都当成 child -> parent的关系利用forest union起来.
再用resHashMap 来记录每一个root 和 这个root对应的所有children, 包括root本身, 对应关系.
最后把resHashMap.values() 挨个排序后加到res中.
Time Complexity: O(nlogn). n=hs.size(). 就是所有点的个数.
得到hs用了O(n). forest union用了 O(nlogn). 得到resHashMap用了O(nlogn). 得到res用了O(nlogn).
Space: O(n).
AC Java:
1 /** 2 * Definition for Directed graph. 3 * class DirectedGraphNode { 4 * int label; 5 * ArrayList<DirectedGraphNode> neighbors; 6 * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) { 11 12 List<List<Integer>> res = new ArrayList<List<Integer>>(); 13 if(nodes == null || nodes.size() == 0){ 14 return res; 15 } 16 17 HashSet<Integer> hs = new HashSet<Integer>(); 18 for(DirectedGraphNode node : nodes){ 19 hs.add(node.label); 20 for(DirectedGraphNode neigh : node.neighbors){ 21 hs.add(neigh.label); 22 } 23 } 24 25 UnionFind forest = new UnionFind(hs); 26 for(DirectedGraphNode node : nodes){ 27 for(DirectedGraphNode neigh : node.neighbors){ 28 forest.union(node.label, neigh.label); 29 } 30 } 31 32 HashMap<Integer, List<Integer>> resHashMap = new HashMap<Integer, List<Integer>>(); 33 for(int i : hs){ 34 //找到root 35 int rootParent = forest.root(i); 36 if(!resHashMap.containsKey(rootParent)){ 37 resHashMap.put(rootParent, new ArrayList<Integer>()); 38 } 39 //每个root下面的值都放在一个list里,包括root本身 40 resHashMap.get(rootParent).add(i); 41 } 42 43 for(List<Integer> item : resHashMap.values()){ 44 Collections.sort(item); 45 res.add(item); 46 } 47 return res; 48 } 49 } 50 51 class UnionFind{ 52 53 //HashMap maintaining key - > value (child -> parent) relationship 54 HashMap<Integer, Integer> parent; 55 public UnionFind(HashSet<Integer> hs){ 56 parent = new HashMap<Integer, Integer>(); 57 for(int i : hs){ 58 parent.put(i, i); 59 } 60 } 61 62 public int root(int i){ 63 while(i != parent.get(i)){ 64 parent.put(i, parent.get(parent.get(i))); 65 i = parent.get(i); 66 } 67 return i; 68 } 69 70 public void union(int i, int j){ 71 int p = root(i); 72 int q = root(j); 73 if(p != q){ 74 parent.put(p, q); 75 } 76 } 77 }
类似Number of Connected Components in an Undirected Graph.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6364620.html
LintCode Find the Weak Connected Component in the Directed Graph相关推荐
- C++实现connected component连通分量(附完整源码)
C++实现connected component连通分量 C++实现connected component连通分量完整源码(定义,实现,main函数测试) C++实现connected compone ...
- C++用并查集Disjoint union实现connected component连通分量(附完整源码)
C++用并查集Disjoint union实现connected component连通分量 C++用并查集Disjoint union实现connected component连通分量完整源码(定义 ...
- 翻译|How to Export a Connected Component
原文在这里:How to Export a Connected Component 根据你在export的不同,可以获得一个完美的函数式React-Redux connected组件,或者是一个完全忽 ...
- Connected component
活动地址:CSDN21天学习挑战赛 Undirected Graph An Undirected graph is connected if, for every pair nodes, th ...
- [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph...
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Codeforces 1196E Connected Component on a Chessboard
https://codeforces.com/contest/1196/problem/E 题解:贪心 /* *@Author: STZG *@Language: C++ */ #include &l ...
- matlab高斯消去法,matlab实现高斯消去法、LU分解
朴素高斯消去法: function x = GauElim(n, A, b) if nargin < 2 for i = 1 : 1 : n for j = 1 : 1 : n A(i, j) ...
- HDU 4635 Strongly connected(缩点、最多可加边数使得仍然非强连通)
整理的算法模板合集: ACM模板 HDU 4635 Strongly connected Give a simple directed graph with N nodes and M edges. ...
- Strongly connected HDU - 4635(tarjan+强连通分量)
题意: 给一个简单有向图,让你加最多的边,使他还是一个简单有向图. 题目: Give a simple directed graph with N nodes and M edges. Please ...
最新文章
- Galaxy生物信息分析平台的数据集对象清理
- [转]C#中的委托和事件(续)
- 关于leetcode第K个最大元素的几种解法
- c语言探测次数不超过4的哈希算法,HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)...
- ACL 2020 | MobileBERT:一种与任务无关的模型压缩方法
- leetcode 67. 二进制求和(C语言)
- Intel Sandy Bridge/Ivy Bridge架构/微架构/流水线 (16) - L1数据缓存/存储转发访存消歧存储体冲突
- DOM基础操作(三)
- 新城建产品 DTCIM
- 【Redis 开发与运维】初识 Redis
- STC15单片机-无线通讯(WIFI模块)
- 运用流体布局的html代码,div+css布局之流体浮动布局_html/css_WEB-ITnose
- PC端微信网页打不开
- qgis二次开发环境
- 百度分享链接批量转存到百度网盘
- python编写程序实现货币转换_使用Tkinter的Python实时货币转换器
- 第一次尝试miniprogram-automator
- 自动化办公1-文件夹文件分类器
- 中国大学MOOC课程《程序设计入门——C语言》翁恺老师 第六周测试题 高精度小数(习题记录)
- java使用枚举法解三元一次方程(百钱白鸡)
热门文章
- dbgrideh指定某单元格变色_一招搞定按指定名称批量新建文件夹
- oracle锁表会话超时时间,ORACLE快速彻底Kill掉的会话,防止锁表
- c语言第一次作业,C语言培训班第一次作业 (1)
- 对口升学计算机组装与维护,对口升学信息技术(计算机)类2017年专业课考试大纲...
- 优化方案电子版_关于小区分支道路整修设计方案的讨论稿(No.2020121)
- python安装界面翻译_python环境搭建
- 两种大小端判断的方式
- python去掉字符串最外侧的引号_疯狂Python讲义第二章读书笔记
- c语言函数调用数组_第七讲:C语言基础之函数,第二节,实现汉诺塔
- 2016_shengyang_onsite