What a crap test Cases and constraints...!! Same solution passing in practice and TLE in contest.
Even it\'s showing better than 75% solutions. \nThis is my solution using BFS.\nFirst I used blue edges to mark nodes visited then tried using green and red edg
Even it\'s showing better than 75% solutions. \nThis is my solution using BFS.\nFirst I used blue edges to mark nodes visited then tried using green and red edges to connect rest nodes and in last connecting different components of blue.\nI know there are so many scope of improvements even I have also submited a improved version but runtime is same for both still I was not able to pass the 82nd case in contest.\n\nLeetcode should do something for this kind of problems.\nTest cases and constraints should be consistent for all the solutions having similar complexity.\n\n\n```\nclass Solution {\npublic:\n    \n    int n;\n    bool pos ;\n    \n    int seprateCount(vector<vector<int>> &gr,vector<vector<int>> goodBlue,vector<bool> vis)\n    {\n        int cnt=0;\n        for(auto &x:goodBlue)\n        {\n            vector<int> ans;\n            for(auto y:x)\n            {\n                \n                for(auto z:gr[y])\n                {\n                    if(vis[z]==false)\n                    {\n                        cnt++;\n                        queue<int> Q;\n                        Q.push(z);\n                        vis[z]=true;\n                        while(Q.size())\n                        {\n                            int cur = Q.front();\n                            Q.pop();\n                            ans.push_back(cur);\n                            for(auto xx: gr[cur])\n                            {\n                                if(vis[xx]==false)\n                                {\n                                    vis[xx]=true;\n                                    cnt++;\n                                    Q.push(xx);\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n            for(auto y:ans)\n            {\n                x.push_back(y);\n            }\n        }\n        \n        for(auto x:vis)\n        {\n            if(x==false)\n            {\n                pos=false;\n                return 0;\n            }\n        }\n        \n        vector<int> mark(n);\n        int m = goodBlue.size();\n        for(int i=0;i<m;i++)\n        {\n            for(auto x:goodBlue[i])\n            {\n                mark[x] = i;\n            }\n        }\n        \n        vector<vector<int>> newGraph(m);\n        for(int i=0;i<n;i++)\n        {\n            int his = mark[i];\n            for(auto x:gr[i])\n            {\n                if(mark[x]!=his)\n                {\n                    newGraph[his].push_back(mark[x]);\n                    newGraph[mark[x]].push_back(his);\n                }\n            }\n        }\n        \n        vector<bool> newVis(m,false);\n        \n        newVis[0]=true;\n        queue<int> Q;\n        Q.push(0);\n        int cnt2 = 0;\n        while(Q.size())\n        {\n            int cur = Q.front();\n            Q.pop();\n            for(auto xx: newGraph[cur])\n            {\n                if(newVis[xx]==false)\n                {\n                    newVis[xx]=true;\n                    cnt2++;\n                    Q.push(xx);\n                }\n            }\n        }\n        \n        if(cnt2<m-1)\n        {\n                pos=false;\n                return 0;\n        }\n        \n        \n        return cnt+cnt2;\n    }\n    \n    int goodBlueCount(vector<vector<int>> &gr,vector<vector<int>> &goodBlue,vector<bool> &vis)\n    {\n        int cnt=0,i;\n        for(i=0;i<n;i++)\n        {\n            if(gr[i].size()&&vis[i]==false)\n            {\n                vector<int> emp;\n                goodBlue.push_back(emp);\n                vector<int> &ans = goodBlue.back();\n                queue<int> Q;\n                Q.push(i);\n                vis[i]=true;\n                while(Q.size())\n                {\n                    int cur = Q.front();\n                    Q.pop();\n                    ans.push_back(cur);\n                    for(auto x: gr[cur])\n                    {\n                        if(vis[x]==false)\n                        {\n                            vis[x]=true;\n                            cnt++;\n                            Q.push(x);\n                        }\n                    }\n                }\n            }\n        }\n        return cnt;\n    }\n    \n\n    bool BFS(vector<vector<int>> &newGraph,vector<bool> newVis)\n    {\n        \n        newVis[0]=true;\n        queue<int> Q;\n        Q.push(0);\n        int cnt2 = 0;\n        while(Q.size())\n        {\n            int cur = Q.front();\n            Q.pop();\n            for(auto xx: newGraph[cur])\n            {\n                if(newVis[xx]==false)\n                {\n                    newVis[xx]=true;\n                    cnt2++;\n                    Q.push(xx);\n                }\n            }\n        }\n        return cnt2==n-1;\n    }\n    \n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n        this-> n =n;\n        int typeCnt[3] = {0,0,0};\n        vector<vector<int>> blue(n);\n        vector<vector<int>> red(n);\n        vector<vector<int>> green(n);\n        for(auto x:edges)\n        {\n            typeCnt[x[0]-1]++;\n            \n            //R\n            if(x[0]==1)\n            {\n                red[x[2]-1].push_back(x[1]-1);\n                red[x[1]-1].push_back(x[2]-1);\n            }\n            //G\n            else if(x[0]==2)\n            {\n                green[x[2]-1].push_back(x[1]-1);\n                green[x[1]-1].push_back(x[2]-1);\n            }\n            //B\n            else\n            {\n                blue[x[2]-1].push_back(x[1]-1);\n                blue[x[1]-1].push_back(x[2]-1);\n            }\n        }\n        pos=true;\n        vector<vector<int>> goodBlue;\n        vector<bool> vis(n,false);\n        int blueCnt = goodBlueCount(blue,goodBlue,vis);\n        if(blueCnt)\n        {\n            int redCnt = seprateCount(red,goodBlue,vis);\n            if(pos==false)\n                return -1;\n\n            int greenCnt = seprateCount(green,goodBlue,vis);\n            if(pos==false)\n                return -1;\n\n            return typeCnt[0]+typeCnt[1]+typeCnt[2]-(blueCnt+redCnt+greenCnt);\n        }\n        else\n        {\n            if(BFS(red,vis)&&BFS(green,vis))\n            {\n                return 0;\n            }\n            return -1;\n        }\n        \n    }\n};\n```\n![image](https://assets.leetcode.com/users/images/828953cc-a57e-4937-8cbf-2b51ef76f34c_1599376737.1800864.png)\n![image](https://assets.leetcode.com/users/images/e1f175aa-c70b-4a66-84c2-a72a372bbf87_1599376737.6449122.png)\n\n
[]