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:

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}

Note

Sort the element in the set in increasing order.

Solution:

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

/**
* Definition for Directed graph.
* struct DirectedGraphNode {
* int label;
* vector<DirectedGraphNode *> neighbors;
* DirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
//use union-set to solve
private:
int find(unordered_map<int, int> &nodeMap, int label){
if(nodeMap.find(label) == nodeMap.end()){
nodeMap[label] = label;
return label;
//if this node doesn't belong to any union-set, create a new set
}else{
//this node belongs to some set, find the root of the set
int res = nodeMap[label];
while(nodeMap[res] != res)
res = nodeMap[res];
return res;
}
}
public:
/**
* @param nodes a array of directed graph node
* @return a connected set of a directed graph
*/
vector<vector<int>> connectedSet2(vector<DirectedGraphNode*>& nodes) {
unordered_map<int, int> nodeMap;
vector<vector<int> > result;
for(int i = ;i < nodes.size();++i){
for(int j = ;j < (nodes[i]->neighbors).size();++j){
int s1 = find(nodeMap, nodes[i]->label);
int s2 = find(nodeMap, (nodes[i]->neighbors)[j]->label);
if(s1 != s2){
//union two sets
if(s1 < s2) nodeMap[s2] = s1;
else nodeMap[s1] = s2;
}else{
//do nothing
}
}
} unordered_map<int, int> setId2VecId;
for(int i = ;i < nodes.size();++i){
int label = nodes[i]->label;
int setId = find(nodeMap, label);
if(setId2VecId.find(setId) == setId2VecId.end()){
vector<int> vec;
setId2VecId[setId] = result.size();
result.push_back(vec);
}
int idx = setId2VecId[setId];
result[idx].push_back(label);
}
for(int i = ;i < result.size();++i)
sort(result[i].begin(), result[i].end()); return result;
}
};

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. 微信也能鉴别山寨iPhone【微信高级教程2】

    现在的技术真的很厉害,iPhone都能山寨几乎一样,外观不用说,系统UI都做得差不多相同,ytkah的一位朋友之前就被人骗了,她拿来手机让我优化,说是很卡,起初ytkah也琢磨很久,只是持怀疑态度,没 ...

  2. /run/systemd/private: No such file or directory

    今天执行consul脚本的时候报错 /run/systemd/private: No such file or directory reboot -f 重启电脑private文件就出现了.

  3. 第11章 使用Vsftpd服务传输文件

    章节简述: 本章节先通过介绍文件传输协议来帮助读者理解FTP协议的用处,安装vsftpd服务程序并逐条分析服务文件的配置参数. 完整演示vsftpd服务匿名访问模式.本地用户模式及虚拟用户模式的配置方 ...

  4. ios流媒体

    http://my.oschina.net/CgShare/blog/302303 渐进式下载(伪流媒体) 介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播放 ...

  5. each-Select

    While Ruby’s each method is useful, it also comes with an awesome extended family of methods that ar ...

  6. 查看远程git log

    $ git reset --hard bit/add $ git log --oneline --graph

  7. ZooKeeper 安装部署

    一.解压 tar -zxvf zookeeper-3.3.5.tar.gz 二.将zookeeper-3.3.4/conf目录下面的 zoo_sample.cfg修改为zoo.cfg,配置文件内容如下 ...

  8. python 最佳入门实践

    勿在浮沙筑高台,无论什么技术,掌握核心精神和api,是很重要的. 但是入门过程也可能不是一帆风顺的,这里有八个入门任务,看看你完成了没有: http://code.tutsplus.com/artic ...

  9. Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup

    如何安装Java,如果出于编程的需要安装Java,需要安装的是JDK,而不仅仅是JRE,下面说说如何在Ubuntu下如何安装JDK:只有两步,1.下载并解压,2.配置环境变量1.下载并解压:下载地址: ...

  10. centos7 安装kvm, 并创建虚拟机

    # yum –y install qemu-kvm qemu-img bridge-utils # yum –y install libvirt virt-install virt-manager # ...