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.pswp.cn/news/393968.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393968.shtml
英文地址,請注明出處:http://en.pswp.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;這些應用程序有一種魔力。但在這篇…