-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path684.cpp
More file actions
39 lines (39 loc) · 1.04 KB
/
684.cpp
File metadata and controls
39 lines (39 loc) · 1.04 KB
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
class Solution {
public:
int n;
vector<vector<int>> graph;
bool bfs(int u, int v){
vector<bool> visited(n+1,false);
queue<int> q;
q.push(u);
visited[u] = true;
while(!q.empty()){
int curr = q.front();q.pop();
for(auto& nxt:graph[curr]){
if(!((curr==u && nxt==v) || (curr==v && nxt==u)) && !visited[nxt]){
visited[nxt]=true;
q.push(nxt);
}
}
}
for(int i=1;i<=n;i++){
if(!visited[i])
return false;
}
return true;
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
n = edges.size();
graph.clear();
graph.resize(n+1);
for(auto& edge:edges){
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}
for(int i=n-1; i>=0; i--){
if(bfs(edges[i][0],edges[i][1]))
return edges[i];
}
return {};
}
};