Union Find
\nclass Solution {\npublic:\n    static bool sortcol(vector<int> &a,vector<int> &b)\n    {\n      return a[0]>b[0];  \n    }\n    int extremeparent(vector<int> 
```\nclass Solution {\npublic:\n    static bool sortcol(vector<int> &a,vector<int> &b)\n    {\n      return a[0]>b[0];  \n    }\n    int extremeparent(vector<int> &parent,int x)\n    {\n      if(parent[x]==x)\n      return x;\n      parent[x]=extremeparent(parent,parent[x]);\n      return parent[x];  \n    }\n    void changeparent(vector<int> &parent,int x,int y)\n    {\n      if(x>y)\n      parent[x]=y;\n      else\n      parent[y]=x;\n      return ;  \n    }\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n        \n       vector<int> parental(n+1,0);\n       vector<int> parentbo(n+1,0);\n       for(int i=0;i<n+1;i++) \n       {\n         parental[i]=i;\n         parentbo[i]=i;\n       }\n       sort(edges.begin(),edges.end(),sortcol);  \n       vector<vector<int>> v=edges;\n       int count=0; \n      // cout<<count<<" "; \n       for(int i=0;i<v.size();i++)\n       {\n         if(v[i][0]==3)\n         {\n          int flag=0;   \n          int x=extremeparent(parental,v[i][1]);\n          int y=extremeparent(parental,v[i][2]);\n          if(x!=y)\n          changeparent(parental,x,y);\n          else\n          flag=1;    \n          x=extremeparent(parentbo,v[i][1]);\n          y=extremeparent(parentbo,v[i][2]);\n          if(x!=y)\n          changeparent(parentbo,x,y);\n          else\n          flag++;\n          if(flag==2)\n          count++;    \n         }\n         else if(v[i][0]==1)\n         {\n          int x=extremeparent(parental,v[i][1]);\n          int y=extremeparent(parental,v[i][2]);\n          if(x!=y)\n          changeparent(parental,x,y);\n          else\n          count++;    \n         }\n         else\n         {\n          int x=extremeparent(parentbo,v[i][1]);\n          int y=extremeparent(parentbo,v[i][2]);\n          if(x!=y)\n          changeparent(parentbo,x,y);  \n          else\n          count++;    \n         }\n         // cout<<count<<" ";  \n       }\n       int last=-1; \n       for(int i=1;i<n+1;i++)\n       {\n         int x=extremeparent(parental,i);\n         if(x==last||last==-1)\n         {\n         }\n         else\n         return -1;\n         last=x;  \n       }\n       for(int j=1;j<n+1;j++)\n       {\n         int x=extremeparent(parentbo,j);\n         if(x==last||last==-1)\n         {\n         }\n         else\n         return -1;\n         last=x;   \n       }\n       return count;\n    }\n};\n\n// \n// \n```
[]