forked from Nandinig24/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1319
75 lines (65 loc) · 2.61 KB
/
1319
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
class Solution {
public:
//.......................... 365 ..................................
//sbse phle saare connected components nikale aur unke vertices aur edges
//min hume vertixes-1 edges cahiye to jis coponent me jyada hi unke extra le lo
// agr n disconnected comp hia to n-1 edges chaiye unhe connect krne ke lie
// connect krne ke lie hume ye bhi dekhna hoga ki kisi compnent me vertices-1 se kam edges to nhi hai agr hai to wo edges bhi hume chaiye
void DFS(int node, vector<vector<int>>& graph, vector<bool>& visited, int& vertices, int& edges) {
visited[node] = true;
vertices++; // Increment vertex count for the current component
for (int neighbor : graph[node]) {
edges++; // Increment edge count for every edge encountered
if (!visited[neighbor]) {
DFS(neighbor, graph, visited, vertices, edges);
}
}
}
vector<pair<int, int>> findComponentsInfo(vector<vector<int>>& graph) {
int n = graph.size(); // Number of nodes
vector<pair<int, int>> componentsInfo; // Stores (vertices, edges) for each connected component
vector<bool> visited(n, false); // Track visited nodes
// Iterate through all nodes in the graph
for (int i = 0; i < n; ++i) {
if (!visited[i]) {
int vertices = 0; // Initialize vertex count for current component
int edges = 0; // Initialize edge count for current component
DFS(i, graph, visited, vertices, edges);
// Store (vertices, edges) count for current component
componentsInfo.push_back({vertices, edges / 2}); // Divide by 2 since each edge is counted twice
}
}
return componentsInfo;
}
int makeConnected(int n, vector<vector<int>>& c) {
vector<vector<int>> ar(n); // Adjacency list
vector<int> vis(n, 0); // Visited array
// Constructing adjacency list
for (auto i : c) {
int a = i[0];
int b = i[1];
ar[a].push_back(b);
ar[b].push_back(a);
}
// Get components information
vector<pair<int, int>> ans = findComponentsInfo(ar);
int comp = ans.size();
int want = comp - 1;
int take = 0;
// Debug print components information
for (int i = 0; i < ans.size(); ++i) {
}
// Calculate additional edges needed or edges that can be reused
for (auto i : ans) {
if (i.first <= (i.second + 1)) {
take += ((i.second + 1) - i.first);
}
// else {
// want += abs(i.second + 1 - i.first);
// }
}
if (take >= want)
return want;
return -1;
}
};