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

Example

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简单了解tengine

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上&#xff0c;针对大访问量网站的需求&#xff0c;添加了很多高级功能和特性。最终目标是打造一个高效、稳定、安全、易用的Web平台。1、基本的HTTP服务器特性1.处理静态文件&#xff0c;索引文件以及自动索引&#xff…

服务器创建多个dhcp服务_如何在15分钟内创建无服务器服务

服务器创建多个dhcp服务by Charlee Li通过李李 如何在15分钟内创建无服务器服务 (How to create a serverless service in 15 minutes) The word “serverless” has been popular for quite a while. When Amazon released the AWS Lambda service in 2015, many tools emerg…

php snoopy视频教程,php的Snoopy类

用了两天这个类&#xff0c;发现很好用。获取请求网页里面的所有链接&#xff0c;直接使用fetchlinks就可以&#xff0c;获取所有文本信息使用fetchtext(其内部还是使用正则表达式在进行处理)&#xff0c;还有其它较多的功能&#xff0c;如模拟提交表单等。使用方法&#xff1a…

网页解析 css

网页解析 css转载于:https://www.cnblogs.com/guozepingboke/p/10792298.html

如何看pg数据库版本号_查看pg数据库版本

PostgreSQL 基本命令链接&#xff1a;http://blog.itpub.net/28602568/viewspace-1841163/标题&#xff1a;PostgreSQL 基本命令作者&#xff1a;&#xff4c;ōττ&#xff52;&#xff59;©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]安装步…

leetcode1091. 二进制矩阵中的最短路径(bfs)

在一个 N N 的方形网格中&#xff0c;每个单元格有两种状态&#xff1a;空&#xff08;0&#xff09;或者阻塞&#xff08;1&#xff09;。一条从左上角到右下角、长度为 k 的畅通路径&#xff0c;由满足下述条件的单元格 C_1, C_2, ..., C_k 组成&#xff1a;相邻单元格 C_i …

lock和synchronized的同步区别与选择

区别如下&#xff1a; 1. lock是一个接口&#xff0c;而synchronized是java的一个关键字&#xff0c;synchronized是内置的语言实现&#xff1b;&#xff08;具体实现上的区别在《Java虚拟机》中有讲解底层的CAS不同&#xff0c;以前有读过现在又遗忘了。&#xff09; 2. syn…

首页显示登陆用户名php,首页登录后怎么在首页显示用户名以及隐藏登录框?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼index.php&#xff1a;登录页面用户名&#xff1a;密码&#xff1a;没有账号&#xff1f;立即注册——————————————————————————doaction.php&#xff1a;header("Content-type:text/html;charsetutf…

react中使用构建缓存_通过在React中构建Tic Tac Toe来学习ReasonML

react中使用构建缓存3. 7. 2018: UPDATED to ReasonReact v0.4.23. 7. 2018&#xff1a;更新为ReasonReact v0.4.2 You may have heard of Reason before. It’s a syntax on top of OCaml that compiles to both readable JavaScript code and to native and bytecode as well…

echart vue 图表大小_vue里echarts自适应窗口大小改变

echarts的图表提供了一个resize方法可以自适应屏幕窗口改变&#xff0c;而重新渲染图表大小的功能。因此我们只要监听浏览器的窗口改变的resize事件&#xff0c;再结合echarts的图表&#xff0c;就可以实现我们想要的功能了。如果是单个图表的情况的话用window.onresize myCha…

用js检测文本框中输入的是否符合条件并有错误和正确提醒

<!DOCTYPE html> <html><head><meta charset"utf-8"><title>捕获异常</title></head><script type"text/javascript">function my_func(){try{xdocument.getElementById("input_id").value;ale…

leetcode784. 字母大小写全排列(回溯)

给定一个字符串S&#xff0c;通过将字符串S中的每个字母转变大小写&#xff0c;我们可以获得一个新的字符串。返回所有可能得到的字符串集合。 示例: 输入: S “a1b2” 输出: [“a1b2”, “a1B2”, “A1b2”, “A1B2”] 输入: S “3z4” 输出: [“3z4”, “3Z4”] 输入: S…

Petapoco使用SQLite的异常问题

在DbProviderFactory 初始化时&#xff0c;报一个"System.Data.SQLite.SQLiteFactory”的类型初始值设定项引发异常。 解决&#xff1a;不光要引用System.Data.SQLite。还要把SQLite.Interop.dll添加到运行目录下。转载于:https://www.cnblogs.com/crazy29/p/7595552.html…

CPP函数调用的方法

相比于C语言中函数可以直接调用&#xff0c;CPP的函数由于命名存在隐式添加&#xff0c;因此需要通过一套流程才能调用&#xff1a; 1. 编码中&#xff0c;使用extern "C" 定义一个C函数&#xff0c;返回获取对象的指针&#xff1b;执行该函数时&#xff0c;获得一个…

php 算法 二进制文件,关于PHP二进制流 逐bit的低位在前算法(详解)_PHP教程

复制代码 代码如下:/******************************************************* 逐bit的低位在前算法* param $x* return int*/function reverse($x){$result 0;for($i 0; $i < 8; $i){$result ($result <> $i));}return $result & 0xff;}调用展示&#xff1a;…

顶尖科技棋牌游戏开发_如何接受顶尖科技公司的采访

顶尖科技棋牌游戏开发If you’ve ever wondered how to land an interview with top tech companies or know someone who’s been struggling to get an interview with one, then this article is for you.如果您曾经想过如何与顶尖高科技公司进行面谈&#xff0c;或者想知道…

城轨列控系统

关于列控系统想问的问题 1&#xff09;列控系统的组成&#xff1f; 2&#xff09;城轨列控系统和列控系统有哪些区别&#xff1f; 3&#xff09;列控系统的设备图片&#xff1f; 4&#xff09;列控系统的作用&#xff1f; 1、地铁的供电部分&#xff1a; 参考&#xff1a;http:…

Thinkphp 发送邮件

TP框架实现发送邮件&#xff0c;亲测可用1.在模块的配置文件config中加入下里面代码THINK_EMAIL > array(SMTP_HOST > smtp.qq.com, //SMTP服务器SMTP_PORT > 465, //SMTP服务器端口SMTP_USER > 邮箱qq.com, //SMTP服务器用户名SMTP_PASS > 密码, //SMTP服务器密…

leetcode40. 组合总和 II(回溯)

给定一个数组 candidates 和一个目标数 target &#xff0c;找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用一次。 说明&#xff1a; 所有数字&#xff08;包括目标数&#xff09;都是正整数。 解集不能包含重复的组合…

python 面部识别_一文教你在Python中打造你自己专属的面部识别系统

原标题&#xff1a;一文教你在Python中打造你自己专属的面部识别系统人脸识别是用户身份验证的最新趋势。苹果推出的新一代iPhone X使用面部识别技术来验证用户身份。百度也在使“刷脸”的方式允许员工进入办公室。对于很多人来说&#xff0c;这些应用程序有一种魔力。但在这篇…