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 ...
随机推荐
- Java开发笔记(一百四十五)FXML布局的伸展适配
前面介绍了FXML的基本格式及其控制器的用法,算是打通了FXML方式的编码流程.程序界面通常保持固定尺寸,不过有时也允许用户拖曳窗口大小,不拖不打紧,一拖就可能坏事.像之前的登录窗口,没拖的时候界面如 ...
- C语言向txt文件写入当前系统时间(Log)
程序实现很简单,涉及到的只有两个知识点. 1.文件最最基本的打开关闭: 2.系统时间的获取. 代码是在VS2019环境下编写的,部分函数在其他编译器中会无法识别.这个就只能需要自己去查找对应的函数简单 ...
- Java并发编程: CountDownLatch、CyclicBarrier和 Semaphore
java 1.5提供了一些非常有用的辅助类来帮助并发编程,比如CountDownLatch,CyclicBarrier和Semaphore. 1.CountDownLatch –主线程阻塞等待,最后完 ...
- golang ---查看进程(Windows)
package main import ( "fmt" "os" "os/exec" "strconv" "s ...
- Oracle 加解密教程
参考Oracle官方文档 在Oracle使用dbms_crypto包进行加解密 首先,授权当前用户使用加解密包 在sql中运行:connect sqlplus as sysdbagrant execu ...
- html 显示 pdf
html 显示 pdf文件四种方式: 1. <embed src="pdf/wobu.pdf" type="application/pdf" width= ...
- Java自学-数字与字符串 字符串
Java中的字符串String 示例 1 : 创建字符串 字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象 常见创建字符串手段: 每当有一个字面值出现的时候,虚拟机就会创 ...
- Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif
Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif 一丶__str__ 和 __repr__ ...
- 换个语言学一下 Golang(14) ——fmt包
Print() 函数将参数列表 a 中的各个参数转换为字符串并写入到标准输出中. 非字符串参数之间会添加空格,返回写入的字节数. func Print(a ...interface{}) (n int ...
- JavaWeb第三天--JavaScript
JavaScript 1. JavaScript概述 1.1 JavaScript是什么?有什么作用? HTML:就是用来写网页的.(人的身体) CSS:就是用来美化页面的.(人的衣服) JavaSc ...