-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximal Network Rank(BFS).cpp
43 lines (38 loc) · 1.15 KB
/
Maximal Network Rank(BFS).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
class Solution {
private:
void bfs(int start, vector<bool>& visited, vector<vector<int>>& adj) {
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int current = q.front();
q.pop();
for (auto& neighbor : adj[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
public:
int maximalNetworkRank(int n, vector<vector<int>>& roads) {
vector<vector<int>> adj(n);
for (int i = 0; i < roads.size(); i++) {
adj[roads[i][0]].push_back(roads[i][1]);
adj[roads[i][1]].push_back(roads[i][0]);
}
int maxi = 0;
vector<int> visited(n, 0);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int val = adj[i].size() + adj[j].size();
if (find(adj[i].begin(), adj[i].end(), j) != adj[i].end()) {
val--;
}
maxi = max(maxi, val);
}
}
return maxi;
}
};