Description

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 weak connected component of a directed graph is a maximum subgraph in which any two vertices are connected by direct edge path.)

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  4. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  6. Connected Component in Undirected Graph

    Description Find connected component in undirected graph. Each node in the graph contains a label an ...

  7. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  8. [HDU6271]Master of Connected Component

    [HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...

  9. 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. BJFU-225-基于链表的两个递增有序序列的合并

    #include<stdio.h> #include<stdlib.h> typedef struct Lnode{ int num; struct Lnode * next; ...

  2. TensorFlow学习笔记——cmd调用方法

    由于tensorflow支持最高的python的版本和anaconda自动配置的python最新版本并不兼容,故直接用常规的在终端键入“python”会出现问题.经过尝试对激活环境,调用的过程暂总结如 ...

  3. 【转】Mac入门(一)基本用法

    我前五年一直外包到微软,每天使用的都是Windows系统和.NET. 2012年加入VMware,  公司的工作机是台Mac 笔记本(MacBook Pro), 所以有机会接触Mac系统 Mac和Wi ...

  4. bypass-media 模式30秒挂断

    语音正常,但是通话30秒后自动挂断, 服务器为阿里云,专网模式 修改ext-sip-ip 为公网ip

  5. .net(2)

    11.interface是否可继承interface?abstract class 是否可实现interface 回答1:接口可以继承接口,而且可以继承多个接口,用“,”进行分割,接口不允许直接或间接 ...

  6. VS 发布MVC网站缺少视图

    mvc项目发布之后会有一些视图文件缺少,不包含在发布文件中,虽然可以直接从项目文件中直接拷贝过来,但还是想知道是什么原因,发布文件好像没有找到哪里有设置这个的地方 把生成操作:无-改成内容即可 原文

  7. C#读写调整设置UVC摄像头画面-亮度

    有时,我们需要在C#代码中对摄像头的亮度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  8. jsMind思维导图模式展示数据

    效果图: jsmind组件下载地址:https://files.cnblogs.com/files/fengyeqingxiang/jsmind.zip 后端代码,此处以C#编写的后台,Java或其他 ...

  9. [echart] webpack中安装和使用

    安装echart npm install echarts --save 全量引入 可以直接在项目代码中 require('echarts') 得到 ECharts. 官方示例 var echarts ...

  10. Maven打包的三种方式

    Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...