Leetcode 6106、统计无向图中无法互相到达点对数

方法一、并查集
class Solution {
public long countPairs(int n, int[][] edges) {
Union union = new Union(n, edges);
for(int[] edge : edges) {
union.union(edge[0], edge[1]);
}
return union.getCount();
}
class Union{
int[] parent;
public Union(int n, int[][] edges) {
parent = new int[n];
for(int i = 0; i < n; i++) {
parent[i] = i;
}
}
public int find(int i) {
if (parent[i] != i) parent[i] = find(parent[i]);
return parent[i];
}
public void union(int x, int y) {
int rootx = find(x);
int rooty = find(y);
if(rooty != rootx) {
if(rootx >= rooty) {
parent[rooty] = rootx;
}else{
parent[rootx] = rooty;
}
}
}
public long getCount() {
long res = 0;
Map<Integer,Integer> cnt = new HashMap<>();
for(int i = 0; i < parent.length; i++){
int root = find(i);
cnt.put(root, cnt.getOrDefault(root,0) + 1);
}
for(int c : cnt.values()){
res += 1L * (parent.length - c) * c;
}
return res / 2;
}
}
}
方法二、DFS
class Solution {
List<List<Integer>> lists = new ArrayList<>();
public long countPairs(int n, int[][] edges) {
long ans = 0;
for(int i = 0; i < n; i++) {
lists.add(new ArrayList<>());
}
for(int[] edge : edges) {
lists.get(edge[0]).add(edge[1]);
lists.get(edge[1]).add(edge[0]);
}
作者:long-xiao-mao
链接:https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/solution/by-long-xiao-mao-m0c9/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
int[] visited = new int[n];
Arrays.fill(visited, Integer.MAX_VALUE);
for(int i = 0; i < n; i++) {
if(visited[i] == Integer.MAX_VALUE) {
dfs(i, visited, i);
}
}
for(int i = 0; i < n; i++) {
if(visited[i] == Integer.MAX_VALUE) {
visited[i] = i;
}
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
map.put((int) visited[i], map.getOrDefault(visited[i], 0) + 1);
}
for(int c : map.values()){
ans += 1L * (n - c) * c;
}
return ans / 2;
}
public void dfs(int index, int[] visited, int groupId) {
if(index >= lists.size() || lists.get(index).size() == 0 || visited[index] == groupId) return;
List<Integer> list = lists.get(index);
visited[index] = groupId;
for(int i = 0; i < list.size(); i++) {
if(visited[list.get(i)] == Integer.MAX_VALUE) {
dfs(list.get(i), visited, groupId);
}
}
}
}
方法三:BFS
class Solution {
List<List<Integer>> lists = new ArrayList<>();
public long countPairs(int n, int[][] edges) {
long ans = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
lists.add(new ArrayList<>());
}
for (int[] edge : edges) {
lists.get(edge[0]).add(edge[1]);
lists.get(edge[1]).add(edge[0]);
}
int[] visited = new int[n];
Arrays.fill(visited, Integer.MAX_VALUE);
for(int i = 0; i < n; i++) {
if(queue.isEmpty()) {
if(visited[i] == Integer.MAX_VALUE) {
queue.offer(i);
visited[i] = i;
}else{
continue;
}
}
while (!queue.isEmpty()) {
int curr = queue.poll();
List<Integer> list = lists.get(curr);
for(int j = 0; j < list.size(); j++) {
if(visited[list.get(j)] == Integer.MAX_VALUE) {
queue.offer(list.get(j));
visited[list.get(j)] = i;
}else {
continue;
}
}
}
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
map.put(visited[i], map.getOrDefault(visited[i], 0) + 1);
}
for(int c : map.values()){
ans += 1L * (n - c) * c;
}
return ans / 2;
}
}