美文网首页
431. Connected Component in Undi

431. Connected Component in Undi

作者: 鸭蛋蛋_8441 | 来源:发表于2019-08-22 08:39 被阅读0次

Description

Find connected component in undirected graph.

Each node in the graph contains a label and a list of its neighbors.

(A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.)

You need return a list of label set.

Nodes in a connected component should sort by label in ascending order. Different connected components can be in any order.

Clarification

Learn more about representation of graphs

Example

Example 1:

Input: {1,2,4#2,1,4#3,5#4,1,2#5,3}

Output: [[1,2,4],[3,5]]

Explanation:

  1------2  3

  \    |  |

    \    |  |

    \  |  |

      \  |  |

        4  5

Example 2:

Input: {1,2#2,1}

Output: [[1,2]]

Explanation:

  1--2

思路:

注意我以为必须连成环才叫连通块,原来只要连在一起就算,走了好多弯路,用全局变量记录访问过的节点,然后BFS每个节点和其邻居,记录在一个tmp临时变量中,最后集中到结果中去。

代码:

相关文章

网友评论

      本文标题:431. Connected Component in Undi

      本文链接:https://www.haomeiwen.com/subject/qejgsctx.html