Find the Weak Connected Component in the Directed Graph
Description
Clarification
graph model explaination:
https://www.lintcode.com/help/graph
Example
Example 1:
Input: {1,2,4#2,4#3,5#4#5#6,5}
Output: [[1,2,4],[3,5,6]]
Explanation:
1----->2 3-->5
\ | ^
\ | |
\ | 6
\ v
->4
Example 2:
Input: {1,2#2,3#3,1}
Output: [[1,2,3]]
思路:
可以把每条有向边看成无向边, 就等同于在无向图中寻找联通块.
两种做法: 1. BFS/DFS 2. 并查集 (但由于输入数据是有向边, 所以使用并查集更合适, 否则还需要先把有向边转换成无向边)
public class Solution {
class UnionFind {
HashMap<Integer, Integer> father = new HashMap<Integer, Integer>();
UnionFind(HashSet<Integer> hashSet) {
for(Integer now : hashSet) {
father.put(now, now);
}
}
int find(int x) {
int parent = father.get(x); while(parent != father.get(parent)) {
parent = father.get(parent);
} return parent;
}
int compressed_find(int x) {
int parent = father.get(x); while (parent != father.get(parent)) {
parent = father.get(parent);
} int temp = -1;
int fa = father.get(x); while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa, parent) ;
fa = temp;
} return parent; } void union(int x, int y) {
int fa_x = find(x);
int fa_y = find(y); if (fa_x != fa_y)
father.put(fa_x, fa_y);
}
} List<List<Integer>> print(HashSet<Integer> hashSet, UnionFind uf) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
HashMap<Integer, List <Integer>> hashMap = new HashMap<Integer, List<Integer>>(); for (int i : hashSet) {
int fa = uf.find(i); if (!hashMap.containsKey(fa)) {
hashMap.put(fa, new ArrayList<Integer>());
} List<Integer> now = hashMap.get(fa);
now.add(i);
hashMap.put(fa, now);
} for (List<Integer> now: hashMap.values()) {
Collections.sort(now);
ans.add(now);
} return ans;
} public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) {
HashSet<Integer> hashSet = new HashSet<Integer>(); for (DirectedGraphNode now : nodes) {
hashSet.add(now.label); for (DirectedGraphNode neighbour : now.neighbors) {
hashSet.add(neighbour.label);
}
} UnionFind uf = new UnionFind(hashSet); for(DirectedGraphNode now : nodes) {
for(DirectedGraphNode neighbour : now.neighbors) {
int fnow = uf.find(now.label);
int fneighbour = uf.find(neighbour.label); if (fnow != fneighbour) {
uf.union(now.label, neighbour.label);
}
}
}
return print(hashSet , uf);
}
}
Find the Weak Connected Component in the Directed Graph的更多相关文章
- [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 ...
- [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 ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
- 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 ...
随机推荐
- 命令(Command)模式
命令模式又称为行动(Action)模式或者交易(Transaction)模式. 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可 ...
- C基本语法
分号 ; 在C程序中,分好是语句结束符,每个语句必须以分好结束,它表明一个逻辑实体的结束 例如: printf("Hello, World! \n"); ; 注释 // 单行注释 ...
- Docker-Compose通过文件声明默认的环境变量
写文目的 在写本文之前,我在做一个docker-compose项目,这里需要在docker-compose.yml配置中引用到宿主机的ip,然而docker-compose并没有命令行一个输入的选项, ...
- excel查找定位操作(for lutai)
产成品出库的单价要根据订单号和存货编码引用产成品入库的单价 方法一:使用Index 和Match =INDEX(产成品入库!I2:P13 ,IF( ) ,7) 方法二:使用vlookup ,首先 ...
- 初步学习async/await,Task.GetAwaiter,Task.Result
网上关于async/await的知识有很多,看了很多但不如自己实践一遍来得快,所以这里记录下我的理解和大家学习下. 首先以最简单的同步方法来开始如下 private static void Test( ...
- C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别 (转载)
多维数组的声明 在声明时,必须指定数组的长度,格式为 type [lenght ,lenght ,lengh, ... ] , ]; 或声明时即赋值,由系统推断长度 int [,] test1 = { ...
- 小程序mpvue怎么点击按钮获取button里面的值
在小程序里面是没有dom元素的,这个我们只要会小程序的应该都知道,但是在平时开发中我们偶尔会遇到需要点击某个元素获取它的值的情况,在这里给大家列举了两种情况解决方法 方式一:数据绑定 这种情况的话,对 ...
- 86.使用webpack爬过的坑
Webpack 4 和单页应用入门 https://github.com/wallstreetcn/webpack-and-spa-guide
- jQuery源码二之extend的实现
extend是jQuery中一个比较核心的代码,如果有查看jQuery的源码的话,就会发现jQuery在多处调用了extend方法. 作用 对任意对象进行扩展 扩展某个实例对象 对jquery本身的实 ...
- Java 数组(三)二维数组
如果一维数组的各个元素仍然是一个数组,那么它就是一个二维数组.二维数组常用于表示表,表中的信息以行和列的形式组织,第一个下标代表元素所在的行,第二个下标代表所在的列. 一.二维数组的创建 1.先声明, ...