Number of Connected Components in an Undirected Graph -- LeetCode

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

Example 1:

     0          3
     |          |
     1 --- 2    4

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:

     0           4
     |           |
     1 --- 2 --- 3

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

思路:并查集(Union find)

 1 class Solution {
 2 public:
 3     int getFather(vector<int>& father, int i) {
 4         if (father[i] == i) return i;
 5         father[i] = getFather(father, father[i]);
 6         return father[i];
 7     }
 8     void merge(vector<int>& father, int i, int j) {
 9         int fatherI = getFather(father, i);
10         int fatherJ = getFather(father, j);
11         father[fatherJ] = fatherI;
12     }
13     int countComponents(int n, vector<pair<int, int>>& edges) {
14         vector<int> father;
15         for (int i = 0; i < n; i++) father.push_back(i);
16         for (int i = 0, n = edges.size(); i < n; i++)
17             merge(father, get<0>(edges[i]), get<1>(edges[i]));
18         unordered_set<int> unions;
19         for (int i = 0; i < n; i++) unions.insert(getFather(father, i));
20         return unions.size();
21     }
22 };
时间: 2024-11-03 21:36:54

Number of Connected Components in an Undirected Graph -- LeetCode的相关文章

[Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. Example 1: 0          3 |          | 1 --- 2    4 Given n = 5 and

Number of Connected Components in an Undirected Graph

1 public class Solution { 2 private int[] parent; 3 public int countComponents(int n, int[][] edges) { 4 if (edges.length == 0) { 5 return n; 6 } 7 parent = new int[n]; 8 for (int i = 0; i < n; i++) { 9 parent[i] = i; 10 } 11 12 for (int i = 0; i <

LeetCode-Number of Connected Components in an Undirected Graph

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. Example 1: 0 3 | | 1 --- 2 4 Given n = 5 and edges = [[0, 1], [1,

[LintCode] Find the Connected Component in the Undirected Graph

Find the Connected Component in the Undirected Graph Find the number connected component in the undirected graph. Each node in the graph contains a label and a list of its neighbors. (a connected component (or just component) of an undirected graph i

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

题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与超级图中的其它顶点相连.) 样例 给定图: A------B C \ | | \ | | \ | | \ | | D E 返回 {A,B,D}, {C,E}.其中有 2 个相连的元素,即{A,B,D}, {C,E} 解题: 广度优先+递归,写不出来,程序来源 Java程序: /** * Defini

uva live 7638 Number of Connected Components (并查集)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5660 题意: 每两个点如果他们的gcd大于1的话就可以连一条边,问在这些数里面有多少个联通块. 题解: 我们可以用筛法倍数.然后用并查集将他们连通起来,2 3 6 本来2 和3的gcd 为1,但是他们可以通过6使得连通. 还有就是要注意 15 25 35 这个数据.

Connected Component in Undirected Graph

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,

Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论

E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x,?y) such that there is no edge between x and y, and if some pair

[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 label and a list of its neighbors. (a connected set of a directed graph is a subgraph in which any two vertices are connected by direct edge path.) Exam