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. Reliable Multicast Programming(PGM)协议

    Reliable Multicast Programming (PGM)实际通用可靠多播协议,在某种程度上保证多播的可靠性.是IP上层协议,和TCP还有UDP同级,工作在传输层. 在组播传输视频项目中 ...

  2. ubuntu docker 搭建 mongodb,开启授权访问 redis,mysql mssql 备份还原

    命令安装docker 如果您想从Ubuntu存储库安装docker版本,则可以运行下面的apt命令. sudo apt install docker.io等到安装完成后,您可以启动Docker并使用s ...

  3. Linux ssh 公私钥配置

    Linux ssh 公私钥配置 ssh 公私钥可实现无密码的情况下直接直接登录到服务端.方便我们管理,而且也可以设置ssh完全通过公私钥登录,不可通过密码登录,来提高我们的服务器安全程度. 配置 生成 ...

  4. Java的常用API之Date类简介

    Data类 java.util.Date:表示日期和时间的类类Date 表示特定的瞬间,精确到毫秒.毫秒:千分之一秒 1000毫秒=1秒 把日期转换成毫秒: 当前的日期:2019-07-18 时间原点 ...

  5. qbittorrent搜索插件合集

    qbittorrent搜索 qbittorrent搜索一个很有特色的功能: 这里收集整理了一些公开网站的插件(Plugins for Public sites),并连 源py文件一起分享. qbitt ...

  6. TRIO-basic指令--CAM

    大家好,今天更新TRIO的运动指令CAM(也就是CAM函数),CAM指令是控制器直接发送编码器脉冲形成的运动曲线,比如:正弦,余弦曲线,根据自己的精度需求进行描点,但并不一定点数越多精度就越高,以实际 ...

  7. django配置文件

    1.BASSE_DIR BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 当前工程的根目录,Django会依 ...

  8. android中如何实现UI的实时更新---需要考虑电量和流量

    1.如果不考虑电量和流量的话,只需要在对应的activity里面继承Runnable,在run方法里面写一个while死循环,调用接口返回数据,如果数据发生了变化,就立即更新UI 2.需要考虑电量的话 ...

  9. Solr基础理论【相关度计算】

    一.简介 寻找匹配的文档是构建优质搜索体验的关键步骤,但这仅仅是第一步.大多数用户不愿意通过逐页翻阅搜索结果来找到想要的文档.根据一般经验,仅有10%的用户在网页搜索中有意愿继续翻阅第一页以后的搜索结 ...

  10. 【Iterm2】如何解决iterm2窗口自动隐藏的问题

    一.问题描述 当我们使用Iterm2的Hotkey Windom功能时,通过快捷键唤起Iterm2窗口后,然后鼠标在iterm2窗口之外触发点击操作就会让 iterm2窗口自动隐藏.. 这样有时候会觉 ...