forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountUnreachablePairsOfNodesInAnUndirectedGraph.java
126 lines (116 loc) · 3.52 KB
/
CountUnreachablePairsOfNodesInAnUndirectedGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/*/
//dfs approach
class Solution {
boolean[] visited;
List<List<Integer>> components;
public long countPairs(int n, int[][] edges) {
visited = new boolean[n];
components = new ArrayList<List<Integer>>();
List<List<Integer>> graph = new ArrayList<List<Integer>>();
int i;
for (i = 0; i < n; ++i) graph.add(new ArrayList<Integer>());
for (int[] edge : edges)
{
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
for (i = 0; i < n; ++i)
{
if (!visited[i])
{
Set<Integer> newComponent = new HashSet<Integer>();
dfs(graph,i,newComponent);
components.add(new ArrayList<>(newComponent));
}
}
long[] pre = new long[components.size()];
pre[0] = components.get(0).size();
for (i = 1; i < pre.length; ++i) pre[i] = pre[i-1]+components.get(i).size();
long result = 0;
for (i = 0; i < pre.length-1; ++i)
result += pre[i]*components.get(i+1).size();
return result;
}
public void dfs(List<List<Integer>> graph, int src, Set<Integer> component)
{
visited[src] = true;
component.add(src);
for (Integer adjNode : graph.get(src))
if (!visited[adjNode])
dfs(graph,adjNode,component);
}
}
//disjoint set solution
class Solution {
boolean[] visited;
public long countPairs(int n, int[][] edges) {
visited = new boolean[n];
DSU dsu = new DSU(n);
for (int[] edge: edges) {
dsu.union(edge[0], edge[1]);
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(dsu.find(i));
}
List<Integer> list = new ArrayList<>();
for (int root: set) {
list.add(dsu.getComponentSize(root));
}
long sum = list.get(0);
long res = 0;
for (int i = 1; i < list.size(); i++) {
res += list.get(i) * sum;
sum += list.get(i);
}
return res;
}
public void dfs(List<List<Integer>> graph, int src, Set<Integer> component)
{
visited[src] = true;
component.add(src);
for (Integer adjNode : graph.get(src))
{
if (!visited[adjNode])
{
dfs(graph,adjNode,component);
}
}
}
}
class DSU {
private int[] parent;
private int[] capacity;
public DSU(int n) {
this.parent = new int[n];
this.capacity = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
capacity[i] = 1;
}
}
public int find(int a) {
int root = parent[a];
while (root != parent[root]) root = parent[root];
while (a != parent[a]) {
a = parent[a];
parent[a] = root;
}
return root;
}
public void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA == rootB) return;
if (capacity[rootA] >= capacity[rootB]) {
capacity[rootA] += capacity[rootB];
parent[rootB] = rootA;
} else {
capacity[rootB] += capacity[rootA];
parent[rootA] = rootB;
}
}
public int getComponentSize(int root) {
return this.capacity[root];
}
}