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. 繁华模拟赛 Vincent的城堡

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  2. 新浪微博的XSS漏洞攻击过程详解

    今天晚上(2011年6月28日),新浪微博出现了一次比较大的XSS攻击事件.大量用户自动发送诸如:“郭美美事件的一些未注意到的细节”,“建 党大业中穿帮的地方”,“让女人心动的100句诗歌”,“3D肉 ...

  3. 第13章 使用Bind提供域名解析服务

    章节简述: 本章节将让您理解DNS服务程序的原理,学习正向解析与反向解析实验,掌握DNS主服务器.从服务器.缓存服务器的部署方法. 够熟练配置区域信息文件与区域数据文件,以及通过使用分离解析技术让不同 ...

  4. [LA4108]SKYLINE

    [LA4108]SKYLINE 试题描述 The skyline of Singapore as viewed from the Marina Promenade (shown on the left ...

  5. Python机器学习库scikit-learn实践

    原文:http://blog.csdn.net/zouxy09/article/details/48903179 一.概述 机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得 ...

  6. Eclipse 项目红色叹号:Build Path Problem

    Description Resource Path Location TypeA cycle was detected in the build path of project 'shgl-categ ...

  7. Java集合框架中List接口的简单使用

    Java集合框架可以简单的理解为一种放置对象的容器,和数学中的集合概念类似,Java中的集合可以存放一系列对象的引用,也可以看做是数组的提升,Java集合类是一种工具类,只有相同类型的对象引用才可以放 ...

  8. HDU4870 Rating(概率)

    第一场多校,感觉自己都跳去看坑自己的题目里去了,很多自己可能会比较擅长一点的题目没看,然后写一下其中一道概率题的题解吧,感觉和自己前几天做的概率dp的思路是一样的.下面先来看题意:一个人有两个TC的账 ...

  9. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  10. angularjs 指令(directive)详解(2)

    原文地址 上一篇我们说到了transclude,那么,我们现在继续讲解之后的内容. 9.scope 可选参数,默认值为false.取值: false - 在这个directive里不会创建新的scop ...