[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 ...
随机推荐
- 错误 1 未知的服务器标记“asp:ScriptManager”。
如题 ... 解决方案 :将 <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=ne ...
- Coursera台大机器学习课程笔记15 -- Three Learning Principles
这节课是最后一节,讲的是做机器学习的三个原则. 第一个是Occan's razor,即越简单越好.接着解释了什么是简单的hypothesis,什么是简单的model.关于为什么越简单越好,林老师从大致 ...
- HTML快速入门4
七.表单 1. 概述 建立交互式的站点,需要使用 HTML 表单,它可以让用户提供信息,并对此作出处理.可以建立类似 复选框.单选按钮及文本框的控件. 掌握表单的使用对 Active Server P ...
- Python字符串与数字互转,数字格式化
# -*- coding: gbk -*- import re #将数字格式化为带三位数逗号的字符串 def formatNumber(number): numStr='%d'%number form ...
- Android 中 设置TextView垂直滚动
布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...
- Firefox上Web开发工具库一览
Firefox的目标之一就是尽可能地使web开发者的生活更简单高效,并通过提供工具和具有很强扩展性的浏览器使人们创造出神奇的东西.使web开发者使用Firefox的时候,浏览器可以提供大量开发工具和选 ...
- MVC 修饰标签
MVC中的修饰标签有很多用途.它以修饰标签形式应用在控制器或控制器中的动作上. 最先想到的就是AcceptVerbs标签,在创建的时候,如果导航到创建视图,但不创建,则: public ActionR ...
- 【转】推荐一款Java反编译器,比较好用
转自:http://www.blogjava.net/xmatthew/archive/2008/10/28/237203.html 推荐一款Java反编译器,也使用了挺久的了,感觉还是很好用,就拿出 ...
- BestCoder15 1002.Instruction(hdu 5083) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary cod ...
- codeforces B. The Fibonacci Segment 解题报告
题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...