-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_2316_CountUnreachablePairsofNodesinUndirectedGraph.cpp
124 lines (101 loc) · 2.86 KB
/
LC_2316_CountUnreachablePairsofNodesinUndirectedGraph.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
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
/*
https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/
2316. Count Unreachable Pairs of Nodes in an Undirected Graph
*/
class Solution {
public:
void dfs(int s, vector<bool> &vis, vector<int>* adj, long long &cnt)
{
if(vis[s]) return;
vis[s] = true;
cnt++;
for(auto &w: adj[s])
if(!vis[w])
dfs(w, vis, adj, cnt);
}
long long countPairs(int n, vector<vector<int>>& edges) {
vector<int> adj[n];
for(auto &x: edges){
adj[x[0]].push_back(x[1]);
adj[x[1]].push_back(x[0]);
}
long long totalpairs = ((long long)n*(n-1))/2;
vector<bool> visited(n, 0);
for(int i=0; i<n; i++)
{
if(!visited[i])
{
long long cnt=0;
dfs(i, visited, adj, cnt);
totalpairs -= (cnt * (cnt-1))/2;;
}
}
return totalpairs;
}
// long long res = 0;
// for (auto i : counts) {
// res += (long long)i * (n-i);
// n -= i;
// }
// return res;
/*
long long countPairs(int n, vector<vector<int>>& edges) {
// vector<vector<int>> adj(n, vector<int>(n, 0));
vector<int> adj[n];
for(auto &x: edges)
{
int i = x[0];
int j = x[1];
// adj[i][j] = adj[j][i] = 1;
adj[i].push_back(j);
adj[j].push_back(i);
}
// for(int i=0; i<n; i++)
// {
// for(int j=0; j<n; j++)
// {
// cout<<adj[i][j]<<" ";
// }
// cout<<endl;
// }
// for(int j: adj[0])
// {
// cout<<j<<" ";
// }
vector<bool> visited(n, 0);
long long int pairs = n;
pairs = (pairs*(pairs-1))/2;
for(int i=0; i<n; i++)
{
vector<int> comp;
long long int compPairs=0;
if(!visited[i])
{
dfs(i, visited, adj, comp);
}
compPairs = comp.size();
compPairs = (compPairs * (compPairs-1))/2;
// for(int x: comp)
// cout<<x<<" ";
// cout<<endl;
pairs -= compPairs;
}
return pairs;
}//
void dfs(int s, vector<bool> &vis, vector<int>* adj, vector<int>& comp)
{
if(vis[s])
return;
vis[s] = true;
comp.push_back(s);
for(auto w: adj[s])
{
// cout<<"s"<<s<<" w"<<w<<" ";
if(!vis[w])
{
dfs(w, vis, adj, comp);
}
}
}
*/
};