[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 ...
随机推荐
- 背景透明的 Dialog
一:控制Dialog 的背景方法:1.定义一个无背景主题主题 <!--去掉背景Dialog--> <style name="NobackDialog" paren ...
- [Effective JavaScript 笔记]第3章:使用函数--个人总结
前言 这一章把平时会用到,但不会深究的知识点,分开细化地讲解了.里面很多内容在高3等基础内容里,也有很多讲到.但由于本身书籍的篇幅较大,很容易忽视对应的小知识点.这章里的许多小提示都很有帮助,特别是在 ...
- 计蒜客 X的平方根
X的平方根 设计函数int sqrt(int x),计算x的平方根. 格式: 输入一个数x,输出它的平方根.直到碰到结束符号为止. 千万注意:是int类型哦- 输入可以如下操作: while(cin& ...
- hiho一下 第九十四周 数论三·约瑟夫问题
数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...
- c3p0数据库连接池
C3P0: 一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 默认情况下(即没有配置连接池的 ...
- 2015安徽省赛 B.求和
题目描述 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1<=n ...
- 【转】INSTALL_FAILED_NO_MATCHING_ABIS 的解决办法
在Android模拟器上安装apk的时候出现 INSTALL_FAILED_NO_MATCHING_ABIS 这个错误提示的解决办法. 是由于使用了native libraries .该nativ ...
- ffplay 2.5.3 媒体播放器
下载地址 http://pan.baidu.com/s/1bnlMYB1 一定要解压到 D:\ffmpeg\ 目录下 双击 OpenWith_FFPlay.reg 注册ffplay 在视频文件名上面, ...
- 【VirtualBox】端口转发,ssh
端口转发 VirualBox的设置 - 网络 - 端口转发 里面有主机IP.主机端口.子系统IP.子系统端口 设置后的含义是:当外部访问主机IP:主机端口后,将会把访问映射到虚拟机的子系统IP和子系统 ...
- 菜鸟学Linux命令:ssh命令 远程登录
1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...