4
There are two cases for the tree structure to be invalid.5
1) A node having two parents;8
If there are both invalid conditions, which means there is a node which has 29
parents and there is also a circle even after we invalid edgeB. In this case,10
we have to return edgeA. only in this way can we avoid both double parents and14
int find(vector<int> &par, int n) {15
if (par[n] == n || par[n] < 0) return n;16
par[n] = find(par, par[n]);20
void findSpecialEdge(vector<vector<int>> &edges, vector<int> &edgeA, vector<int> &edgeB) {21
int len = edges.size();22
vector<int> par(len + 1, 0);23
for (vector<int> e : edges) {24
int p = e[0], c = e[1];35
vector<int> findRedundantDirectedConnection(vector<vector<int>> &edges) {36
int len = edges.size();37
vector<int> parent(len + 1, 0);38
for (int i = 0; i <= len; i++) parent[i] = i;40
vector<int> edgeA = {}; // 1st candidate41
vector<int> edgeB = {}; // 2nd candidate42
findSpecialEdge(edges, edgeA, edgeB);44
for (vector<int> e : edges) {47
if (edgeB.size() > 0 && (edgeB[0] == n1 && edgeB[1] == n2)) { // invalidate edgeB50
int p1 = find(parent, n1);51
int p2 = find(parent, n2);52
if (p1 == p2) { // circle found53
if (edgeB.size() > 0) {