c++ | easy | step by step
\n# Code\n\nclass Solution {\npublic:\n\n\tvector<vector<int>> mem;  \n    bool check(vector<vector<int>>& a, int u, int v) {\n        if(mem[u][v] != 0) return
\n# Code\n```\nclass Solution {\npublic:\n\n\tvector<vector<int>> mem;  \n    bool check(vector<vector<int>>& a, int u, int v) {\n        if(mem[u][v] != 0) return mem[u][v] == 1;\n        for(int i=0; i<a[v].size(); i++) {\n            if(find(a[u].begin(), a[u].end(), a[v][i]) != a[u].end()) {\n                mem[v][u] = mem[u][v] = 1;\n                return true;\n            }\n        }\n        mem[u][v] = mem[v][u] = -1;\n        return false;\n    }\n\t\n    int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {\n        int m = languages.size(), ret = m, i;\n        mem.assign(m + 1, vector<int>(m + 1, 0));\n\t\t\n        for(i=0; i<m; i++)\n            sort(languages[i].begin(), languages[i].end()); \n\t\t\t\n        for(int lang=1; lang<=n; lang++) {\n            int count = 0;\n            vector<bool> teach(m, false);\n            for(i=0; i<friendships.size(); i++) {\n                int u = friendships[i][0] - 1;\n                int v = friendships[i][1] - 1;\n                if(check(languages, u, v)) \n                    continue;\n                if(find(languages[u].begin(), languages[u].end(), lang) == languages[u].end()) {\n                    if(!teach[u])\n                        count++;\n                    teach[u] = true;\n                }\n                if(find(languages[v].begin(), languages[v].end(), lang) == languages[v].end()) {\n                    if(!teach[v])\n                        count++;\n                    teach[v] = true;\n                }\n            }\n            ret = min(ret, count);\n        }\n        return ret;\n    }\n};\n```
['C++']