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. JavaScript中的Function(函数)对象详解

    JavaScript中的Function对象是函数,函数的用途分为3类: 作为普通逻辑代码容器: 作为对象方法: 作为构造函数. 1.作为普通逻辑代码容器 function multiply(x, y ...

  2. String的内存分配

    1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...

  3. iPhone/iOS图片相关(读取、保存、绘制、其它相关)

    http://blog.csdn.net/jerryvon/article/details/7526147 20:50:42 一.读取图片 1.从资源(resource)读取 UIImage* ima ...

  4. 彻底理解数字图像处理中的卷积-以Sobel算子为例

    彻底理解数字图像处理中的卷积-以Sobel算子为例 作者:FreeBlues 修订记录 2016.08.04 初稿完成 概述 卷积在信号处理领域有极其广泛的应用, 也有严格的物理和数学定义. 本文只讨 ...

  5. SQL注入--宽字节注入

    PHP测试代码: <?php // 面向对象写法 $id=addslashes($_GET[‘id’]); //获取id并转义预定义字符 // /$id=$_GET[‘id’]; $mysqli ...

  6. [ruby on rails] 跟我学之(3)基于rails console的查增删改操作

    本章节展开对model的介绍:包括查增删改操作.紧接着上面一节<[ruby on rails] 跟我学之HelloWorld> 创建模型 使用命令创建模型 创建表post,默认自带两栏位 ...

  7. PHP 的__call()

    PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法.如果你试着调用一个对象中不存在或被权限控制中的方法,__call 方法将会被自动调用. 例七:__call ...

  8. 【Hibernate】Hibernate系列4之配置文件详解

    映射文件详解 4.1.概述 4.2.主键生成策略 4.3.属性配置 准确映射: 4.4.映射组成关系 4.5.单向多对一映射 4.6.双向多对一关系 4.7.一对一关联关系-基于外键映射 一对一联合m ...

  9. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  10. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...