-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountUnreachablePairsofNodesinanUndirectedGraph_2316.cpp
66 lines (54 loc) · 1.39 KB
/
CountUnreachablePairsofNodesinanUndirectedGraph_2316.cpp
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
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 2316. Count Unreachable Pairs of Nodes in an Undirected Graph
~ Link : https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/
*/
struct DSU {
int n, comp;
vector<int> parent, compSz;
DSU(int N) {
n = N;
parent.resize(n + 1, -1);
compSz.resize(n + 1, 1);
comp = n;
}
int Find(int x) {
if (parent[x] < 0)
return x;
return parent[x] = Find(parent[x]);
}
bool Merge(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y)
return false;
if (compSz[x] > compSz[y])
swap(x, y);
parent[x] = y;
compSz[y] += compSz[x];
--comp;
return true;
}
};
class Solution {
public:
long long countPairs(int n, vector<vector<int>>& edges) {
DSU dsu(n);
for (auto vec : edges) {
dsu.Merge(vec[0] + 1, vec[1] + 1);
}
unordered_map<int, int> componentSize;
for (int i = 1; i <= n; ++i) {
int parent = dsu.Find(i);
componentSize[parent] = dsu.compSz[parent];
}
long long unreachablePairCount = 0, remNodes = n;
for (auto p : componentSize) {
unreachablePairCount += (p.second * 1ll * (remNodes - p.second));
remNodes -= p.second;
}
return unreachablePairCount;
}
};
// Time Complexity - O(n + e)
// Space Complexity - O(n)