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.)

Example

Given graph:

  1. A----->B C
  2. \ | |
  3. \ | |
  4. \ | |
  5. \ v v
  6. ->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}

Note

Sort the element in the set in increasing order.

Solution:

并查集。遍历每一条变,更新每个节点所在集合。

  1. /**
  2. * Definition for Directed graph.
  3. * struct DirectedGraphNode {
  4. * int label;
  5. * vector<DirectedGraphNode *> neighbors;
  6. * DirectedGraphNode(int x) : label(x) {};
  7. * };
  8. */
  9. class Solution {
  10. //use union-set to solve
  11. private:
  12. int find(unordered_map<int, int> &nodeMap, int label){
  13. if(nodeMap.find(label) == nodeMap.end()){
  14. nodeMap[label] = label;
  15. return label;
  16. //if this node doesn't belong to any union-set, create a new set
  17. }else{
  18. //this node belongs to some set, find the root of the set
  19. int res = nodeMap[label];
  20. while(nodeMap[res] != res)
  21. res = nodeMap[res];
  22. return res;
  23. }
  24. }
  25. public:
  26. /**
  27. * @param nodes a array of directed graph node
  28. * @return a connected set of a directed graph
  29. */
  30. vector<vector<int>> connectedSet2(vector<DirectedGraphNode*>& nodes) {
  31. unordered_map<int, int> nodeMap;
  32. vector<vector<int> > result;
  33. for(int i = ;i < nodes.size();++i){
  34. for(int j = ;j < (nodes[i]->neighbors).size();++j){
  35. int s1 = find(nodeMap, nodes[i]->label);
  36. int s2 = find(nodeMap, (nodes[i]->neighbors)[j]->label);
  37. if(s1 != s2){
  38. //union two sets
  39. if(s1 < s2) nodeMap[s2] = s1;
  40. else nodeMap[s1] = s2;
  41. }else{
  42. //do nothing
  43. }
  44. }
  45. }
  46.  
  47. unordered_map<int, int> setId2VecId;
  48. for(int i = ;i < nodes.size();++i){
  49. int label = nodes[i]->label;
  50. int setId = find(nodeMap, label);
  51. if(setId2VecId.find(setId) == setId2VecId.end()){
  52. vector<int> vec;
  53. setId2VecId[setId] = result.size();
  54. result.push_back(vec);
  55. }
  56. int idx = setId2VecId[setId];
  57. result[idx].push_back(label);
  58. }
  59. for(int i = ;i < result.size();++i)
  60. sort(result[i].begin(), result[i].end());
  61.  
  62. return result;
  63. }
  64. };

Inspired by: http://www.cnblogs.com/easonliu/p/4607300.html

[LintCode] Find the Weak Connected Component in the Directed Graph的更多相关文章

  1. Find the Weak Connected Component in the Directed Graph

    Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...

  2. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  3. [LintCode] Find the Connected Component in the Undirected Graph

    Find the Connected Component in the Undirected Graph Find the number connected component in the undi ...

  4. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  6. Connected Component in Undirected Graph

    Description Find connected component in undirected graph. Each node in the graph contains a label an ...

  7. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  8. [HDU6271]Master of Connected Component

    [HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...

  9. Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)

    E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...

随机推荐

  1. 背景透明的 Dialog

    一:控制Dialog 的背景方法:1.定义一个无背景主题主题 <!--去掉背景Dialog--> <style name="NobackDialog" paren ...

  2. [Effective JavaScript 笔记]第3章:使用函数--个人总结

    前言 这一章把平时会用到,但不会深究的知识点,分开细化地讲解了.里面很多内容在高3等基础内容里,也有很多讲到.但由于本身书籍的篇幅较大,很容易忽视对应的小知识点.这章里的许多小提示都很有帮助,特别是在 ...

  3. 计蒜客 X的平方根

    X的平方根 设计函数int sqrt(int x),计算x的平方根. 格式: 输入一个数x,输出它的平方根.直到碰到结束符号为止. 千万注意:是int类型哦- 输入可以如下操作: while(cin& ...

  4. hiho一下 第九十四周 数论三·约瑟夫问题

    数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...

  5. c3p0数据库连接池

    C3P0: 一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 默认情况下(即没有配置连接池的 ...

  6. 2015安徽省赛 B.求和

    题目描述 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1<=n ...

  7. 【转】INSTALL_FAILED_NO_MATCHING_ABIS 的解决办法

    在Android模拟器上安装apk的时候出现   INSTALL_FAILED_NO_MATCHING_ABIS 这个错误提示的解决办法. 是由于使用了native libraries .该nativ ...

  8. ffplay 2.5.3 媒体播放器

    下载地址 http://pan.baidu.com/s/1bnlMYB1 一定要解压到 D:\ffmpeg\ 目录下 双击 OpenWith_FFPlay.reg 注册ffplay 在视频文件名上面, ...

  9. 【VirtualBox】端口转发,ssh

    端口转发 VirualBox的设置 - 网络 - 端口转发 里面有主机IP.主机端口.子系统IP.子系统端口 设置后的含义是:当外部访问主机IP:主机端口后,将会把访问映射到虚拟机的子系统IP和子系统 ...

  10. 菜鸟学Linux命令:ssh命令 远程登录

    1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...