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 < edges.length; i++) {
13             int aIndex = findParent(edges[i][0]);
14             int bIndex = findParent(edges[i][1]);
15
16             if (aIndex > bIndex) {
17                 parent[aIndex] = bIndex;
18             } else if (bIndex > aIndex) {
19                 parent[bIndex] = aIndex;
20             }
21         }
22
23         int result = 0;
24         for (int i = 0; i < n; i++) {
25             if (parent[i] == i) {
26                 result++;
27             }
28         }
29         return result;
30     }
31
32     private int findParent(int x) {
33         while (x != parent[x]) {
34             parent[x] = parent[parent[x]];
35             x = parent[x];
36         }
37         return x;
38     }
39 }
时间: 2024-08-27 02:37:45

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

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,

[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

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