| 12 | |
| 13 | |
| 14 | class Solution { |
| 15 | public: |
| 16 | int *p,*r; |
| 17 | int find(int x) { |
| 18 | if(p[x]==x) return x; |
| 19 | return p[x]=find(p[x]); |
| 20 | } |
| 21 | void unite(int x,int y) { |
| 22 | x=find(x); |
| 23 | y=find(y); |
| 24 | if(x==y) return ; |
| 25 | if(r[x]>r[y]) { |
| 26 | p[y]=x; |
| 27 | r[x]++; |
| 28 | } else { |
| 29 | p[x]=y; |
| 30 | r[y]++; |
| 31 | } |
| 32 | } |
| 33 | vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) { |
| 34 | int n = edges.size(); |
| 35 | vector<int> inDegree(n+1,-1); |
| 36 | int rem1=-1,rem2=-1; |
| 37 | for(int i=0;i<edges.size();i++) { |
| 38 | vector<int> e=edges[i]; |
| 39 | if(inDegree[e[1]]!=-1) { |
| 40 | rem2=i; |
| 41 | rem1=inDegree[e[1]]; |
| 42 | } |
| 43 | inDegree[e[1]]=i; |
| 44 | } |
| 45 | |
| 46 | p = new int[n+1]; |
| 47 | r = new int [n+1]; |
| 48 | for(int i=0;i<n+1;i++) { |
| 49 | p[i]=i; |
| 50 | r[i]=0; |
| 51 | } |
| 52 | int flag =1; |
| 53 | for(int i=0;i<edges.size();i++) { |
| 54 | if(rem2 == i) continue; |
| 55 | if(find(edges[i][0])==find(edges[i][1])) { |
| 56 | if(rem1!=-1)return edges[rem1]; |
| 57 | else return edges[i]; |
| 58 | } |
| 59 | unite(edges[i][0],edges[i][1]); |
| 60 | } |
| 61 | return edges[rem2]; |
| 62 | |
| 63 | } |
| 64 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected