C++ greedy simulate
\n\n    int minimumTeachings(int n, vector>& languages, vector>& friendships) {\n        \n\t\t\n\t\t// we save each users language id list into map, with id as
\n\n    int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {\n        \n\t\t\n\t\t// we save each users language id list into map, with id as key\n        std::map<int, set<int>> users;\n        for(int i = 0; i < languages.size(); ++i) {\n            for(auto& l : languages[i]) {\n                users[i + 1].insert(l);\n            }\n        }\n        \n\t\t// then we go through each relationship\n\t\t// if a pair of friend don\'t have common language\n\t\t// we save their language and user id into set\n\t\t// we can prove, target language must chosen from lang set\n        set<int> lang;\n        set<int> people;\n        for(auto& f : friendships) {\n            \n            bool skip = false; // if they have one common language, skip is true\n            for(auto& l : users[f[0]]) {\n                if (users[f[1]].count(l) > 0) {\n                    skip = true;\n                    break;\n                }\n            }\n            \n            if (!skip) {\n                for(auto& l : users[f[0]]) { // save user a\'s language into set\n                    lang.insert(l);\n                }\n                for(auto& l : users[f[1]]) { // save user b\'s language into set\n                    lang.insert(l);\n                }\n                \n\t\t\t\t// at the same time, save user a and b into people set\n                people.insert(f[0]);\n                people.insert(f[1]);\n            }\n        }\n        int res = lang.empty() ? 0 : languages.size(); // if no pair of relationship has no common language, the result is 0;\n        \n\t\t\n\t\t// chose any fron lang set, and calculate how many people need taught\n\t\t// save the minimum as result\n        for(auto& l : lang) {\n            int cnt = 0;\n            for(auto& p : people) {\n                if (users[p].count(l) == 0) {\n                    ++cnt;\n                }\n            }\n            res = std::min(res, cnt);\n        }\n        \n        return res;\n        \n    }
[]