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 ...
随机推荐
- 「UNR#2」黎明前的巧克力
「UNR#2」黎明前的巧克力 解题思路 考虑一个子集 \(S\) 的异或和如果为 \(0\) 那么贡献为 \(2^{|S|}\) ,不难列出生产函数的式子,这里的卷积是异或卷积. \[ [x^0]\p ...
- win7系统 右击任务栏 资源管理器 弹出菜单“已固定”和“最近”项目不显示故障处理
故障描述:右击任务栏中资源管理器图标时,弹出菜单只有“资源管理器”和“解除锁定”两个项目,“已固定”和“最近”项目缺失不显示,其他程序均显示正常,只有资源管理器这样.此故障一般发生在经常使用“已固定” ...
- C# LINQ干掉for循环
public void OldSum() { ; ; i < ; i++) { sum0 += i; } Assert.AreEqual(, sum0); } public void NewSu ...
- jwt认证生成后的token如何传回后端并解析的详解
jwt认证生成后的token后端解析 一.首先前端发送token token所在的位置headers {'authorization':token的值',Content-Type':applicati ...
- 输入url之后经历什么?
一.浏览器查找输入域名的IP地址(拿到 IP) 1.查找浏览器缓存(浏览器一般会缓存DNS记录一段时间,一般为2-30分钟). 2.查找系统缓存(即hosts文件,有没有对应的IP) 3.以上都没有的 ...
- France Alternative forms Fraunce
Fraunce See also: france and Francë English France Alternative forms Fraunce In Fraunce, the inhabit ...
- mac下比较好用的svn软件,SVN客户端CornerStone 2.7.10 破解版
一.已经破解,可以直接使用. dmg文件无密码,也不需要注册机.直接使用即可 二. 界面... 三.添加repository 点击左侧栏中REPOSITORY那一栏的+选择添加reposito ...
- Golang Web应用 创建docker镜像笔记(win 平台)
记录的是 本地编译好了再创建容器镜像的方法 ,这样子生成的镜像文件比较小,方便分发部署 win 平台需要设置golang交叉编译 生成linux可执行文件 CMD下: Set GOOS="l ...
- Oracle11g 干净卸载
原文链接:https://www.cnblogs.com/su-root/p/9689787.html 作 者:小柏 出 处:https://www.cnblogs.com/su-root Ora ...
- vue 指令中el 的 parentNode 为空的问题
在项目中,突然发现在用vue指令的时候,发现元素el的父元素parentNode变成了null. 代码: if (el.parentNode && !Vue.prototype.$_h ...