[LintCode] 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.)
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}
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的更多相关文章
- 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 ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
- [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 ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Connected Component in Undirected Graph
Description Find connected component in undirected graph. Each node in the graph contains a label an ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [HDU6271]Master of Connected Component
[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...
- 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 ...
随机推荐
- Apache prefork 模块指令分析
问题背景: 在一台只有内存的vps上安装directadmin之后经常内存耗,经过查看之后发现Apache成了罪魁祸首急速消耗内存SWAP剩余空间都是0,最终导致内核开始大下杀手,把MySQL都杀了, ...
- [2012-4-10]ThinkPHP框架被爆任意代码执行漏洞(preg_replace)
昨日(2012.04.09)ThinkPHP框架被爆出了一个php代码任意执行漏洞,黑客只需提交一段特殊的URL就可以在网站上执行恶意代码. ThinkPHP作为国内使用比较广泛的老牌PHP MVC框 ...
- [Effective JavaScript 笔记]第58条:区分数组对象和类数组对象
示例 设想有两个不同类的API.第一个是位向量:有序的位集合 var bits=new BitVector(); bits.enable(4); bits.enable([1,3,8,17]); bi ...
- Unity3D中定时器的使用
源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...
- git clone报错
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) Could not chdir to home directory /home/wangzheng: ...
- KMP算法精髓
这个算法的做法就是在部分匹配的时候,常规想法是向后移动一位,但是KMP想法是向后移动n位(n=m-L). 注释:这里m表示已经匹配了的字符的个数,L表示已经匹配了的那些字符组成的这个字符串的前缀和后缀 ...
- Android ADB命令大全(通过ADB命令查看wifi密码、MAC地址、设备信息、操作文件、查看文件、日志信息、卸载、启动和安装APK等)
ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootl ...
- C语言指针总结
C语言中的精华是什么,答曰指针,这也是C语言中唯一的难点. C是对底层操作非常方便的语言,而底层操作中用到最多的就是指针,以后从事嵌入式开发的朋友们,指针将陪伴我们终身. 本文将从八个常见的方面来透视 ...
- windows下安装coreseek/sphinx
2013年12月8日 17:26:26 注意的地方: 1.配置文件的 数据源, 索引, 服务 这3处配置的路径要写成windows识别的路径,最好是绝对路径 2.安装windows服务的时候,可以不带 ...
- Windows 8操作技巧之快捷键大全
Windows 8操作系统发布之后,因为其新颖的界面和对触屏友好的设计,使许多长期使用Windows系统的用户,也觉得一时难以适应,一些操作方式也不知道如何去实现.在Windows系统中,快捷键无疑是 ...