-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRedundant Connection.java
43 lines (30 loc) · 1.29 KB
/
Redundant Connection.java
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
/*
Solution by Rahul Surana
***********************************************************
In this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added.
The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.
The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that
there is an edge between nodes ai and bi in the graph.
Return an edge that can be removed so that the resulting graph is a tree of n nodes.
If there are multiple answers, return the answer that occurs last in the input.
***********************************************************
*/
class Solution {
int fp(ArrayList<Integer> p, int x){
if(p.get(x) == x) return x;
return fp(p,p.set(x,p.get(x)));
}
public int[] findRedundantConnection(int[][] edges) {
int n = edges.length;
ArrayList<Integer> p = new ArrayList<>();
for(int i = 0; i <= n; i++) p.add(i);
for(int[] x: edges){
int a = fp(p,x[0]);
int b = fp(p,x[1]);
if(a == b) return x;
p.set(a,b);
}
return new int[0];
}
}