C++ Time O(N^2), Space O(N) || (80 ms,41.5MB) || beats(98.8%,100%)+Notes
\n// count contimination groups (exluding the initials) using the graph\n// if found one from the initial group, add the count to that momber (if more than one 
```\n// count contimination groups (exluding the initials) using the graph\n// if found one from the initial group, add the count to that momber (if more than one skip flag == -2)\n// Go through the initial group get the one with the largest effected count or if equel take lower ix\n// I used here is_intial[i] for flag if is_initial if >0 , for visited if <0 or for counter if is_initial\nclass Solution {\npublic:\n    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {\n        int n = graph.size();\n        int m = initial.size();\n        int is_intial[n];\n        for(int i=0; i<n; i++) is_intial[i] = 0;\n        for(int i=0; i<m; i++) is_intial[initial[i]] = 1;\n        \n        int max_afected = 0; \n        int ans = n;\n\n        int ix = 0;\n        for(int i=0; i<n; i++)\n        {\n            int initial_ix = -1;\n            int group_count = 0;\n            if (is_intial[i] < 0)  // -1 => visited\n                continue;\n            if (is_intial[i] > 0)  // Is initial\n                continue;\n            \n            is_intial[i] = -1;\n            group_count++;\n            // Faster queue\n            int q[n];\n            int q_in = 0;\n            int q_out = 0;\n            q[q_in++] = i;\n            while(q_out < q_in)\n            {\n                int x = q[q_out++];\n                for(int j=0; j<n; j++)\n                {\n                    if (j==x) continue;                   // Self\n                    if (is_intial[j] == -1) continue; // visited\n                    if (!graph[x][j]) continue;       // Not in the group\n                    if (is_intial[j] > 0)                     // From initial group\n                    {\n                        if (initial_ix == -1 || initial_ix == j)  // First in the group or self\n                            initial_ix = j;     // remember first\n                        else\n                            initial_ix = -2;  // Duplicate initial for this group erase single id\n                        continue;\n                    }\n                    group_count++;   // Contimination count\n                    q[q_in++] = j;       // push to queue\n                    is_intial[j] = -1;    // Set Visited flag\n                }\n            }\n            if (initial_ix > -1)          // Updtate count if only one from initial group\n            {\n                is_intial[initial_ix] += group_count;        // update Efected counter\n                if (is_intial[initial_ix] >= max_afected)   // Check if max effected and update response\n                {\n                    if (is_intial[initial_ix] > max_afected  || initial_ix < ans)\n                    {\n                        ans = initial_ix; \n                        max_afected = is_intial[initial_ix];\n                    }\n                }\n            }\n        }\n        \n        if (max_afected == 0)  // no initials have effect other then himself\n        {                                     // find the first graph ix of initial members\n            int i=0;\n            while (is_intial[i] < 1) i++;\n            ans = i;\n        }\n        return ans;\n    }\n};\n/*\n[[1,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,1],[0,0,1,0,0,0,0,0,0],[0,0,0,1,0,0,0,0,1],[0,0,0,0,1,0,1,1,1],[0,0,0,0,0,1,0,0,1],[0,0,0,0,1,0,1,1,0],[0,0,0,0,1,0,1,1,0],[0,1,0,1,1,1,0,0,1]]\n[8,4,2,0]\n*/\n```\n// C++ Time O(N^2), Space O(1)  - Code to Space O(1) Using the input space\n```\n// count contimination groups (exluding the initials) using the graph\n// if found one from the initial group, add the count to that momber (if more than one skip flag == -2)\n// Go through the initial group get the one with the largest effected count or if equel take lower ix\n// I used here is_intial[i] for flag if is_initial if >0 , for visited if <0 or for counter if is_initial\nclass Solution {\npublic:\n    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {\n        int n = graph.size();\n        int m = initial.size();\n        // int is_intial[n];\n        // for(int i=0; i<n; i++) is_intial[i] = 0;\n        for(int i=0; i<m; i++) graph[initial[i]][initial[i]] = 2;\n        \n        int max_afected = 0; \n        int ans = n;\n\n        int ix = 0;\n        for(int i=0; i<n; i++)\n        {\n            int initial_ix = -1;\n            int group_count = 0;\n            int &iii = graph[i][i];\n            if (iii < 1)  // -1 => visited\n                continue;\n            if (iii > 1)  // Is initial\n                continue;\n            \n            iii = -1;\n            group_count++;\n            // Faster queue\n            int q[n];\n            int q_in = 0;\n            int q_out = 0;\n            q[q_in++] = i;\n            while(q_out < q_in)\n            {\n                int x = q[q_out++];\n                for(int j=0; j<n; j++)\n                {\n                    if (j==x) continue;\n                    int &iij = graph[j][j];\n                    if (iij  == -1) continue; // visited\n                    if (!graph[x][j]) continue;  // graph[x][j]\n                    if (iij  > 1)\n                    {\n                        if (initial_ix == -1 || initial_ix == j)\n                            initial_ix = j;\n                        else\n                            initial_ix = -2;  // Duplicate initial for this group\n                        continue;\n                    }\n                    group_count++;\n                    q[q_in++] = j; // push\n                    iij  = -1;\n                }\n            }\n            if (initial_ix > -1)\n            {\n                int &iiix = graph[initial_ix][initial_ix] ;\n                iiix += group_count; // update Efected counter\n                if (iiix >= max_afected)\n                {\n                    if (iiix > max_afected || initial_ix < ans)\n                    {\n                        ans = initial_ix; \n                        max_afected = iiix;\n                    }\n                }\n            }\n        }\n        \n        if (max_afected == 0)\n        {\n            int i=0;\n            while (graph[i][i] < 2) i++;\n            return (i);\n        }\n        return ans;\n    }\n};\n```\n
[]