c++
``` \n\n//the idea is bascially union find // first do union find for type 3 edges and keep  counter of extra edges \n//after that n using the previous union fi
``` \n\n//the idea is bascially union find // first do union find for type 3 edges and keep  counter of extra edges \n//after that n using the previous union find array do union find fortyep 1 and type 2 respecitvely  and inceremnt the count of random  edges \n\nclass Solution {\npublic:  \n    int find(vector<int>&p,int a)\n    {\n      if(p[a]==-1)\n      {\n          return a; \n      }\n       \n      p[a]=find(p,p[a]); \n       \n      return p[a];   \n    }\n    int unionn(vector<int>&p,int a,int b)\n    {\n        int  x,y;  \n        x=find(p,a);  \n        y=find(p,b); \n        \n        if(x==y)\n        {\n            return 1; \n        }\n        \n        p[x]=y;  \n        return 0;  \n        \n    }  \n     int find2(vector<int>&p,int a)\n    {\n      if(p[a]==-1)\n      {\n          return a; \n      }\n       \n      p[a]=find(p,p[a]); \n       \n      return p[a];   \n    }\n    \n        int unionn2(vector<int>&p,int a,int b)\n    {\n        int  x,y;  \n        x=find2(p,a);  \n        y=find2(p,b); \n        \n        if(x==y)\n        {\n            return 1; \n        }\n        \n        p[x]=y;  \n        return 0;  \n        \n    } \n    \n      int solve(int n, vector<vector<int>>& e,int x,vector<int>p)\n    {\n        \n        map<int,bool>vis;  \n        \n        int i,j,k,l,ans=0; \n        \n     \n        for(i=0;i<e.size();i++)\n        {\n            if(e[i][0]==x)\n            {  \n               vis[e[i][1]]=true; \n               vis[e[i][2]]=true;   \n               ans=ans+unionn2(p,e[i][1],e[i][2]);  \n            }\n            if(e[i][0]==3)\n            {\n                vis[e[i][1]]=true; \n               vis[e[i][2]]=true;   \n            }\n        }\n        \n        \n        for(i=1;i<=n;i++)\n        {\n            if(vis[i]==false)\n            {\n                return -1;\n            }\n        }\n        \n\n        \n        return ans;  \n    }\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& e) {\n       \n       vector<int>p(n+1,-1); \n       int  i,j,k,l,ans;  \n       ans=0;  \n        \n       for(i=0;i<e.size();i++)\n       {\n            if(e[i][0]==3)\n            {\n               ans+=unionn(p,e[i][1],e[i][2]);  \n            }\n           \n       }\n       \n       int alice,bob;  \n        \n       alice=solve(n,e,1,p); \n        \n        if(alice==-1)\n        {\n            return -1; \n        }\n        \n        bob=solve(n,e,2,p); \n        \n        if(bob==-1)\n        {\n            return -1;\n        }\n        \n         return ans+alice+bob; \n        \n        \n    } \n};
[]